Page 1 of 1
Display decimal numbers with trailing zeros
Posted: Thu Oct 27, 2005 6:47 am
by php_rook
Hi everybody,
I am new here and first of all: nice to be a member, seems to be helpful and active forum.
So, I am slightly advanced php user, by no means very good
I am just working on an online shop, database driven of course. The
prices of the products are storted in a mysql-database with
data type decimal. Everything runs fine, except
if a product price like 50.00 is stored in the database, and I extract it in the script using php,
php cuts of the trailing zeros and the comma (or point).
It show as "50" instead of "50.00".
How can make php displaying it as 50.00 ? Or do I need to change something in the database ? maybe a different datatype ?
Thanks for any help,
pHp_RoOk(y)
Posted: Thu Oct 27, 2005 6:49 am
by shiznatix
Posted: Thu Oct 27, 2005 6:55 am
by php_rook
Sorry shiznatix,
but I think ceil() and floor() are being used for rounding, hence getting rid of decimal numbers behind comma. My goal is to get them because they get cut off, or are not stored in database.
Prolly I should redefine ym question: what mysql data type leaves the trailing zeros in a nuymber ?
Posted: Thu Oct 27, 2005 6:59 am
by Roja
php_rook wrote:Prolly I should redefine ym question: what mysql data type leaves the trailing zeros in a nuymber ?
DECIMAL, DOUBLE or FLOAT.
http://dev.mysql.com/doc/refman/4.1/en/ ... rview.html
Posted: Thu Oct 27, 2005 7:05 am
by php_rook
I am sure wrote
DECIMAL, DOUBLE or FLOAT.
I am sure, I tried all three of them, but yet, the end of e.g. 50.00 gets cut off while inserting into database...when I try to retrieve it, it shows as 50 instead of 50.00. There must be some other thing to do.
Just dont know what....
Posted: Thu Oct 27, 2005 7:11 am
by Roja
php_rook wrote:I am sure wrote
Its all one word: Roja. Lowercase, as well.. makes it easier to type.
php_rook wrote:I am sure, I tried all three of them, but yet, the end of e.g. 50.00 gets cut off while inserting into database...when I try to retrieve it, it shows as 50 instead of 50.00. There must be some other thing to do.
Just dont know what....
You'll have to show your code. Each of those should maintain the significant digits specified. Of course, you do have to specify the number of significant digits when creating the table.
Thats documented on the mysql page I linked. Did you read it?
Posted: Thu Oct 27, 2005 7:40 am
by Grim...
Ignore what I said before
PHP Manual Comments for 'ROUND' wrote:To round any number to a given number of significant digits, use log10 to find out its magnitude:
round($n, ceil(0 - log10($n)) + $sigdigits);
Or when you have to display a per-unit price which may work out to be less than a few cents/pence/yen you can use:
// $exp = currency decimal places - 0 for Yen/Won, 2 for most others
$dp = ceil(0 - log10($n)) + $sigdigits;
$display = number_format($amount, ($exp>$dp)?$exp:$dp);
This always displays at least the number of decimal places required by the currency, but more if displaying the unit price with precision requires it - eg: 'English proofreading from $0.0068 per word', 'English beer from $6.80 per pint'.
Posted: Thu Oct 27, 2005 8:03 am
by n00b Saibot
if all you care is output
number_format()
Posted: Thu Oct 27, 2005 10:14 am
by onion2k
Personally I think it's better to format the data before it even reaches PHP. Assuming you're using MySQL..
Code: Select all
select *,
FORMAT(price,2) as price_formatted
from
products
Using this method allows you to do things like..
Code: Select all
select *,
FORMAT(price*1.175,2) as price_inc_vat
from
products
As the data is always in the correct format when it leaves the database you'll never need to worry about rounding errors in your PHP code. You'd be amazed how many people will complain if the numbers in their order are out by 1 penny..
Posted: Thu Oct 27, 2005 4:04 pm
by php_rook
Onion 2K:!!!
Brilliant, thanks a lot. THat did the trick, you really grapsed what I needed
Thanks m8, UK rockage
