怎么随机百分比指定字符间的数字

WBOY
Release: 2016-06-20 12:29:34
Original
966 people have browsed it

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); 
Copy after login


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));
Copy after login

source:php.cn
Statement of this Website
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
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!