function foo($m){ return sprintf('%s%.2f', $m[1], $m[2] * ($m[1] == '<value>' ? 0.8: 0.6)); } $p = preg_replace_callback('/(<value>|<price>)([\d.]+)/', "foo", $s);
64.90你好
53.64我好
80.90他好
53.64哈哈
40.90呵呵
53.64噢噢
上面代码是让
之间的数字等于原数字的80%保留小数点两位
之间的数字等于原数字的60%保留小数点两位
但我想让
之间的数字等于原数字的60%,70%,80%,90%随机保留小数点两位
知道的大哥直接给代码谢谢了
回复讨论(解决方案)
rand 乘呗
,
把你算式中的 0.6 改成 rand(6, 9)/10
return sprintf('%s%.2f', $m[1], $m[2] * ($m[1] == '<value>' ? 0.8: rand(6,9)/10));
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn