Function: number_format( )

February 24, 2003 by  
Filed under Code Snippets

Here’s a nice function I learned, number_format().

I was looking for a way to add in the commas for the cost of an item since they were exceeding $1,000. The number $100000.00 is hard to read as opposed to $100,000.00. I thought some parsing would be involved, but I should have know that PHP would already have a function for this. ;)

Here’s a code snippet:
<?php

$value = 100000.00;
echo ‘The cost is $’ . number_format($value, 2) . ‘.’;

?>

Having a ’2′ in the parameters tells the function to return 2 decimal spaces. :) Simple huh? The function comes in handy if you ever have to print out the cost of items in a project.

Related Posts Plugin for WordPress, Blogger...
  • http://www.Sarbar.com Sarbar

    Thank you, I was looking for a code to do some thing just like that.

  • http://www.Sarbar.com Sarbar

    Thank you, I was looking for a code to do some thing just like that.