Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
给定一个数字和 n,我想按以下方式对其进行格式化:
number_format不能做到这一点,实现这一目标的最简单方法是什么? 几个例子:
number_format
输入:数字 = 60,n=1,输出:60 输入:数字=60.0,n=0,输出:60 输入:数字 = 60.0623,n=1,输出:60.0 输入:数字 = 60.06,n=3,输出:60.06
试试这个:
$number = 12345; $maxdecimals = 5; echo rtrim(rtrim(number_format($number,$maxdecimals),"0"),".");
如果后面只有零,这将修剪尾随零和小数点。在这种情况下,您将获得12,345.
12,345
答案是
舍入($number,$maxdecimals)