From eb296b944c5165714c9a163de1006f126621d2cc Mon Sep 17 00:00:00 2001 From: JT Smith Date: Sat, 15 Feb 2003 18:00:37 +0000 Subject: [PATCH] fixed a bug in the round function introduced by tkrahn --- lib/WebGUI/Utility.pm | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/WebGUI/Utility.pm b/lib/WebGUI/Utility.pm index 64a1c6421..e1080e3b2 100644 --- a/lib/WebGUI/Utility.pm +++ b/lib/WebGUI/Utility.pm @@ -283,23 +283,30 @@ sub randomizeHash { #------------------------------------------------------------------- -=head2 round ( real ) +=head2 round ( float [, significantDigits ] ) -Returns an integer after rounding a real number. +Returns an integer after rounding a floating point number. =over -=item real +=item float Any floating point number. +=item significantDigits + +The number of digits to leave after the decimal point. Defaults to 0. + +NOTE: If you set this higher than 0 then you'll get back another floating point number rather than an integer. + =back =cut sub round { - return int( $_ < 0 ? $_ - 0.5 : $_ + 0.5 ); + my $significantDigits = $_[1] || 0; + return sprintf(('%.'.$significantDigits.'f'), $_[0]); } #-------------------------------------------------------------------