2017-03-02 94 views
1

我有價格這樣如何添加自定義格式的價格在PHP

預計輸出

2,125.00

我曾嘗試低於

<?php 
$num ='212500'; 
//setlocale(LC_MONETARY,"en_US"); 
//echo money_format("The price is %i", $num); 
$amount = '212500'; 
setlocale(LC_MONETARY, 'en_IN'); 
$amount = money_format('%!i', $amount); 
echo $amount; 

//echo $formattedNum = number_format($num, 2, ',', ''); 
+0

您已更改值本身。 –

+0

可以告訴mw如何實現這個? –

+0

如果數字是212501.如果 –

回答

2

根據您的要求,您可以做到這一點。 first divide the number by 100 and then change the format

$amount = 212501; 
echo number_format($amount/100, 2, '.', ',');//2,125.01 
1

使用給定的方法

function moneyformat($amount) 
    { 
     $amount = trim($amount); 
     $amount = $amount/100; 
     return number_format($amount, 2, '.', ','); 
    } 
$amount = 212500; 
echo moneyformat($amount); //Output is 2,125.00 
$amount = 212501; 
echo moneyformat($amount); //Output is 2,125.01 
+0

可以ü請檢查我的預期輸出 –

+0

嘗試編輯答案.... –