2013-10-18 27 views
1

我想格式化爲1000到10.00 PHP number_format函數似乎不適用於此。格式化數字從1000到10.00

我曾嘗試:

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

任何想法?有沒有辦法我可以manupulate number_format來顯示結果(即只在最後兩位數字前插入一個小數?)

回答

0

number_format的第三個參數應該是你想用作小數點的字符。 ?一個空字符串,你爲什麼把你的電話號碼($cost)在字符串中

試試這個:echo number_format($cost,2,'.',',');

編輯:也許我誤解你的問題 - 如果你要顯示的號碼1000 10.00,只需將$cost除以100,然後致電number_format()

0

這對你來說合法嗎?

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

1000和10.00是完全不同的數字(值)。除以100,然後正確格式化:

$cost = 1000 ; 
$cost /= 100 ; 

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

試試這個代碼: 「」

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

數字格式將改變到一個「,」但你告訴它格式一萬。

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

你想要什麼簡單地說就是:

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

Reallu非常有用!謝謝。 –