Re: Formatting Decimals Further Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Jan 22, 2010, at 4:24 PM, tedd wrote:

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.


Hi Ted.

This is exactly what I am trying to do, some of the values in the DB are going to have 3 decimals, some 2 and some 1.

On my page I pull the value from the db with:

$my_price = $row['my_price'];

This returns 3.80... even though my db has it as 3.8. No problem. But once I run the variable $my_price through the following line of code, it is truncating the 0:

$my_price = round_to_half_cent(number_format($my_price, 3, '.', ','));

Again, the above call to the custom function works fine for 2 and 3 decimal points.... just not 1 decimal point. Because I call this function over many pages, I would prefer if possible to fix it at the custom function level rather than recode each page. But I will do whatever is necessary. I afraid with my current understanding of PHP I am not able to successfully modify the custom function to tack on a 0 when the decimal place is empty. Will keep trying and post if I am successful

Thanks,

--Rick




I'm afraid with my current understanding of PHP, I am not able to come up with the logic to

 --Rick



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux