PHP - i want my number to appear 1,234,567.89 - Asked By Olusegun A. on 28-Oct-09 02:58 PM

Hello there,

Please i to display the amount in my database to appear in currency unit. for example if what i have in my database is 123456789, i want to echo it as 1,234,567.89 that is full-stop after the first two figures and comma after every 3 characters

Regards
Olusegun

You can use the PHP number_format function to format your numbers. - [)ia6l0 iii replied to Olusegun A. on 28-Oct-09 09:24 PM

Like, 
$inputNumber = 12345.56;
$outputNumber = number_format($inputNumber,2);

However, for much more control over the formatting i would use the money_format function. It adheres to specific region locale and then formats the input for you. I can't recollect the exact syntax right now. Do a serachin your favorite engine for the exact syntax.

Venkat K replied to Olusegun A. on 29-Oct-09 12:19 AM

Use number_format - Sagar P replied to Olusegun A. on 29-Oct-09 12:44 AM

To format the number in US currency, use PHP’s built-in “number_format” function like so:


<?php

// Our original decimal number

$number = 21357.44;

// Let’s use PHP’s built-in function to format the number into US currency

$formatted = number_format($number,2);

// The following statement will print 21,357.44

echo $formatted;

?>

As you can see, not too hard at all. I passed the number_format function two parameters: “$number,” which is the variable I want to format, and “2,” which is the amount of decimal places I want to display. We did forget one thing, though– the dollar sign ($) in front of our number. This is easily solved by:


<?php

// Our original decimal number

$number = 21357.44;

// Let’s use PHP’s built-in function to format the number into US currency

$formatted = number_format($number, 2);

// The following statement will print 21,357.44

echo "\$" . $formatted;

?>