我想将 1000 格式化为 10.00PHP number_format 函数似乎不适用于此。
我试过:
$amount2 = number_format("$cost",2,"",",");
echo "$cost";
有什么想法吗?有没有办法可以操纵number_format来显示结果(即只是在最后两位数字之前插入小数?
数字格式会将"."更改为",",但您告诉它格式化一千。
$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00
你想要的很简单:
$cost=1000;
echo number_format($cost/100,2,'.',',');
//10.00
这对
你来说是合法的吗?
<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00
1000 和 10.00 是完全不同的数字(值)。除以 100,然后正确格式化:
$cost = 1000 ;
$cost /= 100 ;
$amount2 = number_format($cost,2,".","");
echo $amount2 ;
试试这段代码:
$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2; // Displays 10.00
要number_format
的第三个参数应该是要用作小数点的字符。为什么要传递空字符串?为什么要把你的数字($cost
)放在一个字符串里?
试试这个: echo number_format($cost,2,'.',',');
编辑:也许我误解了你的问题 - 如果您希望数字1000显示为10.00,只需在呼叫number_format()
之前将$cost
除以100即可。