Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Zmniejszanie o 1 przy konwersji z float na int
Forum PHP.pl > Forum > Przedszkole
mundeck
  1. $kwota = 1305.34; //float(1305.34) (OK)
  2. $kwota_100 = $kwota*100; // float(130534) (OK)
  3. $modulo = 20000; // int(20000) (OK)
  4. $kwota_100_int = (int) $kwota_100; // int(130533) DLACZEGO NIE int(130534)?questionmark.gif
  5. $mod = $kwota_100_int % $modulo; // int(10533) DLACZEGO NIE int(10534)?questionmark.gif


sadistic_son
Cytat
This is because numbers that have a finite representation in base 10 may or may not have an exact representation in the floating point representation PHP uses.

See
  1. >php -r "echo var_dump(sprintf('%.40F', 39.3 * 100.0));"
  2. string(45) "3929.9999999999995452526491135358810424804688"

Since int allows rounds the number down, a small error in the representation makes the cast round it one number down that you would otherwise expect.

Consider using round instead.

http://stackoverflow.com/questions/3385685...o-int-type-cast
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.