Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding 3

Status
Not open for further replies.

perlTest

Programmer
Nov 16, 2002
14
0
0
US
How do I round so that 4.6000 will turn into 5? Or anything before 4.5000 turn into the next number. Like 1.5000 will be 2.0000. Thank you for your help.

-Matt
 
$number = 2.6;

$rounded = sprintf("%.0f", $number);

print "$rounded\n";
Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
assumed: value = 4.6
round = (value * 10) % 10;
round = (10 * (round >4) - round) / 10
# ^^^^^^^^____returns 1 if true
# (10-6)/10 = 0.4
# 4.6 + 0.4 = 5.0
value += round;
#gives: 5.0
---------------------------------------
assumed: value = 4.2
round = (value * 10) % 10;
round = (10 * (round >4) - round) / 10
# ^^^^^^^^____returns 1 if true
# (0-2)/10 = -0.2
# 4.2 + -0.2 = 4.0
value += round;
#gives: 4.0
---------------------------------------
i propose:

round = (value * 10) % 10;
round = 0 if(round == 5);
round = (10 * (round >4) - round) / 10 if(round != 0);
value += round;

will return:
4.0 if value was 4.4
4.5 if value was 4.5
5.0 if value was 4.6 -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
MikeLacey: sorry mike, you are just cutting decimals :(
this is not really rounding... -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Just use the int function after you have added 0.5.

e.g.

4.6 + 0.5 = 5.1 -> int(5.1) = 5
4.49 + 0.5 = 4.99 -> int(4.99) = 4

Bye Werner
 
Look again Jamisar, I checked my code before posting and it rounded correctly on my machine. Mike

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
let's see....

$number = 25.623;
@splitnumber = split (/\./,$number);
if (substr($splitnumber[1],0,1) >= 5){
$rounded = $splitnumber[0]+1;
}else{
$rounded = $splitnumber[0]
}
print $rounded;
 
OpenWater has it right, the absolute fastest way to round is to add 0.5 and truncate. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Out of curiosity, I performed a crude test to see the difference in speed by rounding the same number 10 million times...
OpenWater's method was indeed the fastest at a blistering 16 seconds.

it seems my silly clusterf**k of a program wasn't as slow as I thought it would be - it was tied with MikeLacey's method at about 46 seconds.

the PC is a P4 1.8GHz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top