PHP : fonction "round" comportement bizarre

tarass

Grand Maître
Voila ce qui m'arrive je souhaite arrondir à deux chiffre aprés la virgule un nombre:
echo (round("9.815", 2)."<BR>");
echo (round("609.815", 2)."<BR>");
echo (round("4609.815", 2)."<BR>");
echo (round("14609.815", 2)."<BR>");
echo (round("6.735", 2)."<BR>");
echo (round("56.735", 2)."<BR>");
echo (round("1056.735", 2)."<BR>");
echo (round("11056.735", 2)."<BR>");

resultat:
9.82
609.82
4609.81
14609.82
6.74
56.74
1056.73
11056.74

pourquoi ".82" devient ".81" et ".74" devient ".73"
Si quelqu'un connait une fonction php qui se plante pas sur l'arrondi je prend
avaez-vous le meme problème en executant ce code sur votre serveur. j'utilise : PHP Version 4.4.1

j'ai mis ca sur free:

<?
echo (round("9.815", 2)."<BR>");
echo (round("609.815", 2)."<BR>");
echo (round("4609.815", 2)."<BR>");
echo (round("14609.815", 2)."<BR>");
echo (round("6.735", 2)."<BR>");
echo (round("56.735", 2)."<BR>");
echo (round("1056.735", 2)."<BR>");
echo (round("11056.735", 2)."<BR>");
echo phpinfo();
?>
pareil
 

tarass

Grand Maître
bon ba round c'est pourris surtout ne pas s'en servir pour de la compta le resultat est aléatoire....
 

marsien

Grand Maître
C'est une bonne chose à savoir... Si c'est confirmé, mais ça semble l'être d'après mes quelques tests...

D'un autre côté, rien ne t'empèche de faire ta propre fonction ;)
 

tarass

Grand Maître
clair :)
l'astuce de sioux vu sur php.net:

$delta = 0.00001;
$price1 = round($price1+$delta, 2);

et hélas ca marche.


raison invoqué :
"Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is read this:


Thank you for your interest in PHP.
"

en gros ils savent que la fonction est foireuse mais ils ne l'indique pas dans la documentation ca fait pas trés sèrieux....
 
Vous devez vous inscrire ou vous connecter pour répondre ici.
Derniers messages publiés
Statistiques globales
Discussions
730 128
Messages
6 717 845
Membres
1 586 373
Dernier membre
https://forum.tomshardwar
Partager cette page
Haut