Reputation: 239
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.
Upvotes: 10
Views: 31610
Reputation: 440
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;
Upvotes: 1
Reputation: 7327
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"
Upvotes: 2
Reputation: 598
$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"
Upvotes: 0
Reputation: 479
<?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
?>
Upvotes: 5
Reputation: 2769
Check number_format, here is an example
echo number_format(8333*1000, 3, ',', '.');
Upvotes: 2
Reputation: 33532
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.
Upvotes: 33