PERL - rounding fractional number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting PERL - rounding fractional number
# 1  
Old 11-19-2010
Lightbulb PERL - rounding fractional number

It seems that perl sprintf uses the round-to-even method:
PHP Code:
foreach my $i 0.51.52.53.5 ) {
    
printf "$i -> %.0f\n"$i;
}
__END__

0.5 
-> 0
1.5 
-> 2
2.5 
-> 2
3.5 
-> 4
4.5 
-> 
Where we probably wants to use round-half-up, i.e. output should be as below:

PHP Code:
0.5 -> 1
1.5 
-> 2
2.5 
-> 3
3.5 
-> 4
4.5 
-> 
Any suggestion to correct the above code to accomplish the above required result?
# 2  
Old 11-19-2010
use the POSIX module and you'll have access to the ceil() and floor() functions.
# 3  
Old 11-19-2010
Hi Pludi,

Thanks a lot for your torch light and solution.

Cheers ~~ / Mysore 101 Ganapati.
# 4  
Old 11-19-2010
The mathematical method is to add 0.5 and then ignore the decimal place.
# 5  
Old 11-19-2010
Question

Another question arises here:

If I use floor(), as:

PHP Code:
#!/usr/bin/perl
use POSIX;
foreach 
my $i 0.40.51.21.62.12.9) {
    
printf "$i -> %.0f\n"floor($i);
}
__END__ 
resulting output as:
PHP Code:
0.4 -> 0
0.5 
-> 0
1.2 
-> 1
1.6 
-> 1
2.1 
-> 2
2.9 
-> 

If I use ceil(), as:

PHP Code:
#!/usr/bin/perl
use POSIX;
foreach 
my $i 0.40.51.21.62.12.9) {
    
printf "$i -> %.0f\n"ceil($i);
}
__END__ 
resulting:

PHP Code:
0.4 -> 1
0.5 
-> 1
1.2 
-> 2
1.6 
-> 2
2.1 
-> 3
2.9 
-> 

How to roundup the values like,

PHP Code:
if ($any_number >= 0.5then {
    
roundup to its higher number
} else {
    
roundup to its lower number

i.e,

result should be as below:

PHP Code:
0.4 -> 0
0.5 
-> 1
1.2 
-> 1
1.6 
-> 2
2.1 
-> 2
2.9 
-> 
Is there any dedicated built-in function in PERL, to accomplish this? Smilie
# 6  
Old 11-19-2010
Short answer: no.

Long answer: See perlfaq4
# 7  
Old 11-19-2010
Install the Perl Math::Round module and use the round() function.

Code:
#!/usr/bin/perl

use Math::Round;

foreach my $i ( 0.4, 0.5, 1.2, 1.6, 2.1, 2.9) {
    printf "$i -> %.0f\n", round($i);
}

This is the output
Code:
0.4 -> 0
0.5 -> 1
1.2 -> 1
1.6 -> 2
2.1 -> 2
2.9 -> 3

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[awk] rounding a float number?

Heyas Trying to calculate the total size of a file by reading its bitrate. Code snippet: fs_expected() { # # Returns the expected filesize in bytes # pr_str() { ff=$(cat $TMP.info) d="${ff#*bitrate: }" echo "${d%%,*}" | $AWK '{print $1}' | head -n 1 } t_BYTERATE=$((... (9 Replies)
Discussion started by: sea
9 Replies

2. Shell Programming and Scripting

Rounding off to the nearest floating number

I have a number, which I want to convert into the nearest floating number upto two places after the decimal point. E.g. 1.2346 will become 1.23 but 1.2356 will become 1.24 . Similarly 0.009 will be 0.01 and 0.001 will be 0.00 or 0.0 (not 0, wnat to keep the decimal... (1 Reply)
Discussion started by: hbar
1 Replies

3. Shell Programming and Scripting

printf (awk,perl,shell) float rounding issue

Hi guys, could someone throw some light on the following behaviour of printf (I'll start with info about the system and the tool/shell/interpreter versions)?: $ uname -a Linux linux-86if.site 3.1.0-1.2-desktop #1 SMP PREEMPT Thu Nov 3 14:45:45 UTC 2011 (187dde0) x86_64 x86_64 x86_64... (9 Replies)
Discussion started by: elixir_sinari
9 Replies

4. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

5. Shell Programming and Scripting

Using Fractional Numbers in Shell Variables

Hello everyone, Before I state my problem I would like to inform you that this is not an assignment. I'm just trying to learn some shell programming in my free time. I'm trying to write a parking fee calculator. But I don't seem to be able to use fractional numbers. Could you please advice... (6 Replies)
Discussion started by: iwant2learn
6 Replies

6. UNIX for Dummies Questions & Answers

Rounding up to nearest whole number

Hi all of you, Would be great if you help me with how to round up to whole number from my input values like 2.99996,2.17890,3.00002,-2.3456,-2.7890 o/p should be like 3,2,3,-2,-3 thnks in adv!!!! regards (3 Replies)
Discussion started by: Indra2011
3 Replies

7. Shell Programming and Scripting

Rounding number, but....

Dear Experts, I'm trying to find a way to round a number but in this way: 14367.577 ---> 14000 I used the following to round the number to the closer integer: echo $var|awk '{print int($1+0.5)}' and also: xargs printf "%1.0f" However, they don't work for my above... (9 Replies)
Discussion started by: Gery
9 Replies

8. Shell Programming and Scripting

rounding time in perl

Is there a way I can round time in perl to the nearest five minutes? For example if I have log giving the following time stamps 23,52,30 it would rounded up to 23,55,00 and 23,50,01 would be rounded to 23,50,00 (3 Replies)
Discussion started by: borderblaster
3 Replies

9. Shell Programming and Scripting

Rounding off decimals to the nearest number in PERL

Hi Guys, I am generating a statistical report , below is the snippet of the code : Now, $nSlices stands for the time duration,meaning,the statistics will be displayed for that particular time duration. Trouble is, for certain values of $totalTime (which is the end time - start time ), i... (9 Replies)
Discussion started by: rdlover
9 Replies

10. Shell Programming and Scripting

Rounding off to the next whole number

Hello, I searched a lot on this Forum. Please help me with the below problem. I want to divide two numbers and the result should be the next nearest whole number. E.G. Dividing 10.8/5 ideally gives 2.16. But the result should be 3 i.e. rounded off to the next whole number. Any help will... (2 Replies)
Discussion started by: damansingh
2 Replies
Login or Register to Ask a Question