![]() |
|
#1
|
||||
|
||||
The mystery of 9.95I have this script that will accept a price off a web form. Since it's a price, I prefer to save the information to the database as an integer, in other words, in cents.
For some strange, unknown reason, when I test the script and submit a price like 9.95, the script will end up with 994 cents! Here's an example script you can run yourself: PHP Code:
If you submit 9.95 or 8.95, the results will be 994 and 894 respectively. However anything else will be fine, e.g. 6.95, 7.95, 3.95 etc. I thought it was a Windows issue... but I find the same result(s) testing the example script above, on this web server (Linux). It can't be the browsers either. Same results on both, FF and IE. Anybody with a suggestion? __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#2
|
||||
|
||||
Re: The mystery of 9.95What about just a straight:
PHP Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
|
#3
|
||||
|
||||
Re: The mystery of 9.95The problem has to do with floating point precision; see the manual page for PHP discussing this very point. I am sure there is an infinite number of numbers exhibiting this problem, and floating point precision is a problem that plagues any computer programming language with finite representation of floating point numbers - just about everyone there is, considering that the limitation is hardware-related; software developers are perfect. I think there are several implementations in the ML family of PL's that allow for a different handling, and I seem to remember Ruby had something like this, but I imagine you don't care about this
Can you consider acquiring the input data into a string and then performing a simple parsing to determine the number in the "integer" form? Best regards, Lucian __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#4
|
||||
|
||||
Re: The mystery of 9.95TurboPT,
Thank you for the suggestion but I didn't notice a change. Just in case I misunderstood you, here's what I edited in the example script. PHP Code:
Lucian, I read that page actually, but I was just annoyed because I couldn't understand why it was happening randomly. Why would 9.95 turn into 994 but 6.95 works out fine into 695! Can I just add 0.000001 to the value? You know, as a quick fix? __________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#5
|
||||
|
||||
Re: The mystery of 9.95Quote:
No, that wouldn't really be a fix, since you would still perform operations on floating point numbers, and that is prone to rounding errors. I would recommend reading What Every Computer Scientist Should Know About Floating-Point Arithmetic, but I understand you are in a real hurry to solve the problem. I am also sorry for throwing links at you, but I feel like I am really not qualified to explain those things myself, as I find them to be hardly straight-forward. I can tell you that some time ago Intel actually had to recall an entire series of processors which displayed gross rounding errors for floating point numbers, but otherwise they are accepted as a limitation of the current hardware, which only works with base 2 numbers. Long story short, wouldn't it be easier to just accept the data as a string, e.g: "12.345" and parse the string as: "12" "345" than make "12345" out of it and convert that to an integer? You know I don't program in Php __________________
Please read these Guidelines before posting on the forum "A person who never made a mistake never tried anything new." Einstein |
|
#6
|
||||
|
||||
Re: The mystery of 9.95Brilliant! Thank you Lucian. Simple, and does the job!
__________________
J de Silva Learning Journal | GIDForums™ | GIDNetwork™ | GIDWebhosts™ | GIDSearch™ |
|
#7
|
||||
|
||||
Re: The mystery of 9.95JdS,
I was thinking more along the lines of: [without using intval()] PHP Code:
__________________
Use the force...read the source!! WYCIWYG -- what you code is what you get! |
Recent GIDBlog
2nd Week of IA Training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Search Engine Optimization mystery | saxena | Search Engine Optimization Forum | 2 | 05-Apr-2006 07:42 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The