Formatting numbers from 1000 to 10.00

自作多情 提交于 2019-12-11 02:35:54

问题


I'll like to format 1000 to 10.00 The PHP number_format function does not seem to be working for this.

I have tried:

$amount2 = number_format("$cost",2,"",",");
echo "$cost";

Any ideas? Is there a way I can manupulate number_format to display the results (i.e just inserting a decimal before the last two digits?


回答1:


Number format will change the "." to a "," but you telling it to format ONE THOUSAND.

$cost=1000;
echo number_format($cost,2,'.',',');
//1,000.00

What you want is simply:

$cost=1000;
 echo number_format($cost/100,2,'.',',');
//10.00



回答2:


Is this legit for you ?

<?php
$cost=1000;
echo substr($cost, 0, 2) . "." . substr($cost, 2);//10.00



回答3:


1000 and 10.00 are totally different numbers (in values). Divide by 100, then format it properly:

$cost = 1000 ;
$cost /= 100 ;

$amount2 = number_format($cost,2,".","");
echo $amount2 ;



回答4:


Try this code:

$stringA= 1000;
$length=strlen($stringA);
$temp1=substr($stringA,0,$length-2);
$temp2=substr($stringA,$length-2,$length);
echo $temp1.".".$temp2;     // Displays  10.00



回答5:


The third parameter to number_format should be the character you want to use as a decimal point. Why are you passing an empty string? And why are you placing your number ($cost) inside a string?

Try this: echo number_format($cost,2,'.',',');

EDIT: Perhaps I misunderstood your question — if you want the number 1000 to be displayed as 10.00, just divide $cost by 100 before calling number_format().



来源:https://stackoverflow.com/questions/19445746/formatting-numbers-from-1000-to-10-00

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!