php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #39088 round()
Submitted: 2006-10-09 07:54 UTC Modified: 2006-11-06 09:23 UTC
From: msajko at yahoo dot com Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: Irrelevant OS: Windows XP
Private report: No CVE-ID: None
 [2006-10-09 07:54 UTC] 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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-09 07:58 UTC] 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-11-06 09:23 UTC] msajko at yahoo dot com
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);
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 08:01:27 2024 UTC