Re: Converting floats to ints with intval

From: Date: Thu, 06 May 2010 11:03:16 +0000
Subject: Re: Converting floats to ints with intval
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-304948@lists.php.net to get a copy of this message
On 06/05/10 11:52, Paul Waring wrote:
Ashley Sheridan wrote:
Why don't you store them as integer values and add in the decimal point with something like sprintf() afterwards? Store the values as pence and then you won't have any rounding problems.
If I was designing the system from scratch, that's what I'd do. Unfortunately this is an add-on to a legacy system where currency values are already stored as strings in the database (yes, not ideal I know, but you have to work with what you've got).
Can you not just add up the floating point numbers and the round them with round() (http://www.php.net/manual/en/function.round.php) Certainlay for consistent calculations I'd always use the float values for as long as possible... Otherwise, why not multiply by 1000 before taking the intval, then at least you preserve the next decimal place.

« previous php.general (#304948) next »