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