Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show prices as million and thousands format? [closed]

Tags:

php

I have the following prices.

  • 14,500,000
  • 1,800,000

How these prices can be shown as millions and thousands in PHP ?

I want to have a user defined function to show these values as millions and thousands format as these prices will be dynamic.

Updated

I want to show the above values as:

  1. 14,500,000 will be as 14 millions and 500 thounsands
  2. 1,800,000 will be as 1 million and 800 thounsands.

So anyone can tell me how it can done so ?

Thanks.

like image 705
user2826169 Avatar asked Oct 09 '13 06:10

user2826169


1 Answers

Use number_format() function

echo number_format("14500000", 0, '', ',');

Output

14,500,000


OR

If you want to represent price in Kilo/Million/Billion

$number = "14500000";

if ($number < 1000000) {
    // Anything less than a million
    $format = number_format($number);
} else if ($number < 1000000000) {
    // Anything less than a billion
    $format = number_format($number / 1000000, 2) . 'M';
} else {
    // At least a billion
    $format = number_format($number / 1000000000, 2) . 'B';
}

echo $format;

Output

14.50M
like image 82
Sumit Bijvani Avatar answered Sep 29 '22 16:09

Sumit Bijvani