Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I format a number to a dollar amount in PHP

How do you convert a number to a string showing dollars and cents?

eg: 123.45    => '$123.45' 123.456   => '$123.46' 123       => '$123.00' .13       => '$0.13' .1        => '$0.10' 0         => '$0.00' 
like image 436
nickf Avatar asked Nov 17 '08 04:11

nickf


People also ask

How do you convert numbers to money format?

On the Home tab, click the Dialog Box Launcher next to Number. Tip: You can also press Ctrl+1 to open the Format Cells dialog box. In the Format Cells dialog box, in the Category list, click Currency or Accounting. In the Symbol box, click the currency symbol that you want.

What format is used for dollar amounts?

United States (U.S.) currency is formatted with a decimal point (.) as a separator between the dollars and cents. Some countries use a comma (,) instead of a decimal to indicate that separation.

How can I get 2 decimal places in PHP?

$twoDecNum = sprintf('%0.2f', round($number, 2)); The rounding correctly rounds the number and the sprintf forces it to 2 decimal places if it happens to to be only 1 decimal place after rounding.


1 Answers

If you just want something simple:

'$' . number_format($money, 2); 

number_format()

like image 75
Darryl Hein Avatar answered Sep 22 '22 09:09

Darryl Hein