Sponsored Content
Top Forums Shell Programming and Scripting Rounding off decimals to the nearest number in PERL Post 302302056 by KevinADC on Monday 30th of March 2009 12:56:12 AM
Old 03-30-2009
Quote:
Originally Posted by rdlover
Below is the snippet of the code that i have written. The damn sprintf is not working. No matter what number i enter, it is entering ceil and not floor.Any reasons why ?



Code:
#!/usr/bin/perl

# SOME VARIABLE

use POSIX;

print "Enter a number = ";

$nSlices1 = <STDIN>;

($bdec, $adec ) = split( /\./, $nSlices1 ); # Split the variable at the decimal point and put it into 2 temporary variables


print " The value of numbers before decimal point is $bdec \n";

sprintf("%.3f", $adec) ;

print " The value of numbers after  decimal point is $adec \n";


if ($adec >= 50)

{
     $ceil = ceil($nSlices1); 
     print "The value of $nSlices1 has been rounded off to $ceil \n";
    } 

else 

{
      $floor = floor($nSlices1);
        
       print "The value of input has been rounded off to $floor  \n";
        
}

Code:
#!/usr/bin/perl

# SOME VARIABLE

print "Enter a number = ";

$nSlices1 = <STDIN>;
chomp $nSlices1;
print " The value of numbers before decimal point is $nSlices1 \n";

$nSlices1 = sprintf("%d", $nSlices1);

print " The value of numbers after  decimal point is $nSlices1 \n";

 

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

removing and rounding up decimals

Hi Experts, I have a command that gives me the output as below root@ckpgpay11core> cat sara | awk '{ sum += $1} ; END { print sum }' | awk {'print $1/90'} 8.88889 how do i remove the decimal spaces so that the figure will round itself to 9? Thanks. (3 Replies)
Discussion started by: aismann
3 Replies

3. Shell Programming and Scripting

convert Regular decimals to Packed decimals

Hi, I am trying to find if there is a way to convert regular decimal values to Paced decimal values. I tried to find a c program but I could get a Packed converted to regular decimal not the other way round. If not unix please let me know if any other progrimming language I can use to do... (2 Replies)
Discussion started by: mgirinath
2 Replies

4. 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

5. Shell Programming and Scripting

PERL - rounding fractional number

It seems that perl sprintf uses the round-to-even method: foreach my $i ( 0.5, 1.5, 2.5, 3.5 ) { printf "$i -> %.0f\n", $i; } __END__ 0.5 -> 0 1.5 -> 2 2.5 -> 2 3.5 -> 4 4.5 -> 4 Where we probably wants to use round-half-up, i.e. output should be as below: 0.5 -> 1 1.5 -> 2... (8 Replies)
Discussion started by: ganapati
8 Replies

6. Shell Programming and Scripting

AWK Match to nearest number

Hello Guys, I'm very new on here and require some help matching up and printing some columns using awk. I have two text files. The first file has Longitude data in column 1 (lon.txt) and the second one (node.txt) has again another Longitude data in column 1 (not exact as the first one) + in... (7 Replies)
Discussion started by: ian_gooch
7 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. 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

9. 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

10. 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
vrstat(1M)																vrstat(1M)

NAME
vrstat - display statistical information for volumes in RVGs and RLINKs, and for all hosts in an RDS SYNOPSIS
vrstat [-g diskgroup] [-V] [-S] [-R] [-M] [-n number] [rvg ...] DESCRIPTION
The vrstat command prints statistical information for the volumes in Replicated Volume Groups (RVGs) and RLINKs, and for all hosts in a Replicated Data Set (RDS). Information is displayed across the RDS setup on all the hosts, and not for a specific host. By default, the command displays statistics at intervals of 10 seconds. This interval can be changed by setting the VRAS_STATS_FREQUENCY environment variable to the required value in the /etc/vx/vras/vras_env file. If no rvg argument is specified, the command displays information for the RLINKs, storage replicator logs (SRLs), data volumes or memory tunables across all the RDSs on the local host, depending on the option that is specified. If no option or argument is specified, the vrstat command displays the consolidated status for the RLINKs, SRLs, data volumes and memory tunables of all RDSs on the local host. For more information on the output formats, see the vxrlink(1M), vxstat(1M) and vxmemstat(1M) manual pages. OPTIONS
-g diskgroup Specifies the disk group for the operation. If the -g option is not specified, the rvg argument determines the default local disk group, according to the standard disk group selection rules that are described on the vxdg(1M) manual page. -M Displays detailed information for the memory tunables on every host in an RDS. The output from this option is similar to that from the vxmemstat command. -n number Displays the statistics number times, and then exits. -R Displays RLINK-related statistical information for all hosts in an RDS. The output from this option combines the output from the vxrlink stats and vxrlink status commands, and also displays bandwidth utilization by the RLINK. This output can be used to diagnose connectivity and network problems. -S Displays statistical information for SRL volumes on all hosts in an RDS. -V Displays statistical information for data volumes on all hosts in an RDS. EXIT CODES
The vrstat utility exits with a status of 0 if the attempted operation succeeds. It exits with a status of 1 if the attempted operation fails. This exit code is the same for all errors. EXAMPLES
Display statistical information for all RLINKs in an RDS across all hosts: vrstat -R egrvg Display consolidated statistical information for all RLINKs, SRLs, data volumes and memory tunables for all RDSs on a host: vrstat SEE ALSO
vxmemstat(1M), vxrlink(1M), vxstat(1M) VxVM 5.0.31.1 24 Mar 2008 vrstat(1M)
All times are GMT -4. The time now is 01:38 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy