Forum Moderators: coopster & phranque

Message Too Old, No Replies

Rounding to the nearest hundredth

Severe migrane

         

adni18

12:11 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How can a number be rounded to the nearest hundredth (x.xx) in use strict or use cgi?

upside

1:03 am on Oct 7, 2004 (gmt 0)

10+ Year Member



Math::Round is great for this. The following code will round a number to the nearest hundredth. For example, it will round 9.12345678 to 9.12


use Math::Round;
my $number = 9.12345678;
my $output = nearest(.01,$number);

upside

1:11 am on Oct 7, 2004 (gmt 0)

10+ Year Member



hmm I just realized that I've already recommend this module to you once before at

[webmasterworld.com...]

adni18

11:33 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes, and I tried alternating the script to 0.01 instead of one and that didn't work

killroy

11:55 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



U can always do it yourself, multiply by 100, convert to integer, convert back to float and divide by 100.

SN

PCInk

12:09 pm on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



or use the string print function:

$x = sprintf("%0.2f",$x);