Re: why does 69.99*100=6998?



The Natural Philosopher wrote:
Betikci Boris wrote:
On Feb 1, 5:59 pm, The Natural Philosopher <a...@xxx> wrote:
The question says it all.

I have an input box, which I fill in with a price.
IOt gets passed to the main form as a variable, then shoved into an SQL
field vue a print '%d' statement where the argument is $price*100.

For some reaosn, this particular value goes to 6998.

If I update hee database manually to 6999, it displays as 69.99.

If I enter 69.999 it updates as 6999..not 69999

I must be doing something really dumb here..

it's simply a rounding error! because type float is weak while storing
the numbers with floating points you should store values using type
double or you can divide each part of the number ex. for 89.98 as 89
and 98 you could do this using php's bundled string manipulation
functions..

No,what it is is a fundamental flaw in the thinking of whoever designed the language, to consider that converting from a float to an integer would be a matter of TRUNCATION rather than rounding.


How does the language know that you want your result rounded. You are failing to understand that even though your input is (string)69.99, the second you convert it to a float you are losing precision. PHP doesn't know what your intentions are with the data. PHP has no way of knowing that you want a rounded result unless you tell it so... nor does it have any way of knowing that the original value was 69.99.

round($float [,$precision])

check round function on php.net for a lot of examples.

--
Norman
Registered Linux user #461062
.



Relevant Pages

  • Re: Breaking backwards compatibility - good or bad?
    ... coming and are needed to anything really big and useful in PHP. ... command or statement in the language that is going to be made invalid ... The PHP team tries very hard not to break CORRECT scripts. ... "Introducing case-sensitivity for variables and functions names has ...
    (comp.lang.php)
  • Re: Case sensitivity in programming languages.
    ... the language. ... And these conventions take advantage of case sensitivity. ... In PHP it can be any of those things. ... group of programmers who want t force their stupid ideas onto others. ...
    (comp.lang.php)
  • Re: A Lot of Questions from a Noob
    ... OOP is a language paradigm that is not specific to ... I Google'd for an introduction to object-oriented programming, ... Watch out for the die-hard Perl fanbois: ... PHP 5 OOP has come a long way from PHP 4. ...
    (comp.lang.php)
  • Re: Ideal computer language from scratch?
    ... Takes 3 float as input, outputs 2 float, nicely formatted in bracktes ... subject and glancing over a volume on the language, ... zipWith takes a function and applies it to 2 lists, ... fibs and. ...
    (alt.lang.asm)
  • Re: Case sensitivity in programming languages.
    ... Is any language issued with a set of function names in different ... You get a warning if you reference an undeclared variable in PHP if your ... In something like PHP we could generate a convention ... The ability to have 3 different functions called readfile(), ...
    (comp.lang.php)

Loading