2017-03-02 64 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

尝试编辑答案.... –