如何在php中添加自定义格式的价格

时间:2017-03-02 06:41:17

标签: php

我有这样的价格

212500

预期输出

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, ',', '');

2 个答案:

答案 0 :(得分:2)

根据您的要求,您可以执行此操作。first divide the number by 100 and then change the format

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

答案 1 :(得分: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