Re: Formatting Decimals Further Help

From: Date: Fri, 22 Jan 2010 21:24:28 +0000
Subject: Re: Formatting Decimals Further Help
References: 1 2 3 4 5 6 7  Groups: php.general 
Request: Send a blank email to php-general+get-301362@lists.php.net to get a copy of this message
Hello List. In an earlier post, I received help with a custom function to round decimals off (the custom function provided by Adam Richardson is below). However in my MySQL db, when I have values with only 1 decimal point, I need the value PHP returns to display as 2. For example, 3.8 needs to display as 3.80. My line of code that calls the custom function looks like this: $my_price = round_to_half_cent(number_format($my_price, 3, '.', ',')); When the value of $my_price is 3.81, it returns 3.81. However, when the value of $my_price is 3.8 that is what it returns. How can I force the formatting of my_price to always contain either 2 or 3 decimal points (3 if the original number contains 3 or more decimal points to begin with) Thanks, --Rick
Rick: Okay so 3.8 is stored in the database and not as 3.80 -- but that's not a problem. What you have is a display problem so use one of the many PHP functions to display numbers and don't worry about how it's stored. Cheers, tedd -- ------- http://sperling.com http://ancientstones.com http://earthstones.com

« previous php.general (#301362) next »