#39088 [Bgs]: round()

From: Date: Mon, 06 Nov 2006 09:23:05 +0000
Subject: #39088 [Bgs]: round()
References: 1  Groups: php.doc 
Request: Send a blank email to phpdoc+get-969374331@lists.php.net to get a copy of this message
ID: 39088 User updated by: msajko at yahoo dot com Reported By: msajko at yahoo dot com Status: Bogus Bug Type: Documentation problem Operating System: Windows XP PHP Version: Irrelevant New Comment: I can't belive that I will have to use my round functions in every new aplication I made? :-( function roundit($val,$precision=0){ $num=$val+(('0.'.str_repeat('0',$precision).'5')*1); $a=explode(".",$num); return ($a[0].'.'.substr($a[1],0,$precision))*1; } function numberformat($number,$decimals='2',$dec_point='.',$thousands_sep=','){ $number=roundit($number,$decimals); return number_format($number,$decimals,$dec_point,$thousands_sep); } Previous Comments: ------------------------------------------------------------------------ [2006-10-09 07:58:17] derick@php.net 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: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. . ------------------------------------------------------------------------ [2006-10-09 07:54:14] msajko at yahoo dot com Description: ------------ round(555.305,1) = 555.3 round(555.305,2) = 555.3 !!!? (555.31 is correct) round(555.305,3) = 555.305 problem appears in Windows binary, on debian binary it works just fine Reproduce code: --------------- echo round(555.305,2) ."\n"; echo (round(555.305,2)*1)."\n"; Expected result: ---------------- 555.31 555.31 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=39088&edit=1

« previous php.doc (#969374331) next »