[PHP] Converting floats to ints with intval

2010-05-06 Thread Paul Waring
I've got the following script which demonstrates a problem I'm having with floating point numbers and intval: $times100 = (-37.12 * 100); print $times100 . \n; $intval100 = intval($times100); print $intval100 . \n; print ($intval100 / 100) . \n; I expect the output to be: -3712 -3712 -37.12

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Ashley Sheridan
On Thu, 2010-05-06 at 11:24 +0100, Paul Waring wrote: I've got the following script which demonstrates a problem I'm having with floating point numbers and intval: $times100 = (-37.12 * 100); print $times100 . \n; $intval100 = intval($times100); print $intval100 . \n; print

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Paul Waring
Ashley Sheridan wrote: It's part of the rounding problem you get with most languages out there. Why can't you compare the floating point values though? Currency should only have one decimal place anyway. You can't compare floating point values because if you have, for example, a user-entered

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Ashley Sheridan
On Thu, 2010-05-06 at 11:40 +0100, Paul Waring wrote: Ashley Sheridan wrote: It's part of the rounding problem you get with most languages out there. Why can't you compare the floating point values though? Currency should only have one decimal place anyway. You can't compare floating

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Paul Waring
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

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Pete Ford
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,

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread David Otton
On 6 May 2010 11:52, Paul Waring p...@xk7.net wrote: 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

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Paul Waring
David Otton wrote: On 6 May 2010 11:52, Paul Waring p...@xk7.net wrote: 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

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread David McGlone
On Thursday 06 May 2010 07:19:48 Paul Waring wrote: David Otton wrote: On 6 May 2010 11:52, Paul Waring p...@xk7.net wrote: 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

RE: [PHP] Converting floats to ints with intval

2010-05-06 Thread Bob McConnell
From: David McGlone On Thursday 06 May 2010 07:19:48 Paul Waring wrote: David Otton wrote: On 6 May 2010 11:52, Paul Waring p...@xk7.net wrote: 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

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Paul Waring
David McGlone wrote: On Thursday 06 May 2010 07:19:48 Paul Waring wrote: David Otton wrote: On 6 May 2010 11:52, Paul Waring p...@xk7.net wrote: 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

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread David McGlone
On Thursday 06 May 2010 08:39:03 Bob McConnell wrote: From: David McGlone On Thursday 06 May 2010 07:19:48 Paul Waring wrote: David Otton wrote: On 6 May 2010 11:52, Paul Waring p...@xk7.net wrote: If I was designing the system from scratch, that's what I'd do. Unfortunately this is

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Robert Cummings
Paul Waring wrote: I've got the following script which demonstrates a problem I'm having with floating point numbers and intval: $times100 = (-37.12 * 100); print $times100 . \n; $intval100 = intval($times100); print $intval100 . \n; print ($intval100 / 100) . \n; I expect the output to be:

Re: [PHP] Converting floats to ints with intval

2010-05-06 Thread Fernando
I think Bob has a good idea here, split the string values and the concatenate them to make the whole value. If the data is really stored in strings, you need to break it down into substrings around the decimal and then convert both sides into integers and combine them into an integer value. It