Formatting price as comma separated

自闭症网瘾萝莉.ら 提交于 2020-01-03 07:30:08

问题


In my database I have values like

256.23, 200.33, 89.33, 133.45,

I have to multiply these values with thousand and then format the result as price(comma separated)

256.23 x 1000 = 256230            I want to show this as            256,230

200.33 x 1000 = 200330            I want this as                    200,330

89.33  x 1000 = 89330             I want this as                    89,330

Currently I am using formula

echo "Price is : $".$price*1000;

But how to format this, I've no idea.


回答1:


You are looking for the number_format function.

$price=123456;
echo number_format($price);
// output: 123,456

This function accepts either one, two, or four parameters (not three):

If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands.

If two parameters are given, number will be formatted with decimals decimals with a dot (".") in front, and a comma (",") between every group of thousands.

If all four parameters are given, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals and thousands_sep instead of a comma (",") between every group of thousands.




回答2:


<?php

$number = 1234.56;

// english notation (default)
$english_format_number = number_format($number);
// 1,235

// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>



回答3:


Check number_format, here is an example

echo number_format(8333*1000, 3, ',', '.');



回答4:


$number = 1234.56;

setlocale(LC_MONETARY,"en_US");

echo money_format("The price is %i", $number);

//output will be "The price is USD 1,234.56"




回答5:


The answers above do not account for decimals or rounding, this may be helpful to people who need to worry about decimals:

Examples: show no decimals use spaces instead of commas, and print with decimal and commas:

$price = 1000000.90;
var_dump(number_format(floor((float) $price), 0, ',', ' '));
var_dump(number_format($price, 2, '.', ','));

Output:

string(9) "1 000 000"
string(12) "1,000,000.90"



回答6:


Here is the custom function to convert price into Indian Price format using PHP 7+

function moneyFormatIndia($num) {
    $explrestunits = "" ;
    if(strlen($num)>3) {
        $lastthree = substr($num, strlen($num)-3, strlen($num));
        $restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
        $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
        $expunit = str_split($restunits, 2);
        for($i=0; $i<sizeof($expunit); $i++) {
            // creates each of the 2's group and adds a comma to the end
            if($i==0) {
                $explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
            } else {
                $explrestunits .= $expunit[$i].",";
            }
        }
        $thecash = $explrestunits.$lastthree;
    } else {
        $thecash = $num;
    }
    return $thecash; // writes the final format where $currency is the currency symbol.
}


$amount = '10000034000';
$amount = moneyFormatIndia( $amount );
echo $amount;


来源:https://stackoverflow.com/questions/11649866/formatting-price-as-comma-separated

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