Rounding number, but....


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rounding number, but....
# 1  
Old 04-27-2011
Rounding number, but....

Dear Experts,

I'm trying to find a way to round a number but in this way:

Code:
14367.577 ---> 14000

I used the following to round the number to the closer integer:

Code:
echo $var|awk '{print int($1+0.5)}'

and also:

Code:
xargs printf "%1.0f"

However, they don't work for my above example.

Please, any hints and support will be very grateful.

Best regards,
# 2  
Old 04-27-2011
Try:
Code:
echo $var|awk '{x=int($1+0.5);sub("...$","000",x);print x}'

# 3  
Old 04-27-2011
Code:
num=14367.577
printf "%2.2s000\n" "$(( ${num%.*} / 1000 ))"

# 4  
Old 04-27-2011
Java

Dear bartus11 and Dear cfajohnson,

Many thanks for your answers, I tried both and the one that it's close to what I need is bartus11's code. However, I forgot to say that I have a range of values from 0 to 100 000. In fact, this code worked well for most of my values (and also more than 100 000), but I have problems in the range between 99.5 and 999.4. For example, if I run the code with:

Code:
var=99.4
echo $var|awk '{x=int($1+0.5);sub("...$","000",x);print x}'

then I get 99, which is very good.

However, from 99.5 to 999.4, I get 000. In this case, for instance, I expect to have with the number 459.3244, just 450.

In the case this code coulnd't be modified to cover the whole range (from 0 to 100 000), how could the script be modified to cover the range from 99.5 to 999.4?

I'm very grateful for your excellent support, many thanks again,

Best regards,
# 5  
Old 04-27-2011
Hammer & Screwdriver Confused by your rounding rules

Please document the rules, and provide some examples.
Your initial question looks to round to the nearest 1000.
# 6  
Old 04-27-2011

This requires bash or ksh93:
Code:
#!/bin/bash
var=${1:-12345.6789}
num=${var%.*}
pad=$(( ${#num} - 2 ))
sig=${num:0:2}
printf "%d%0${pad}d\n" "$sig" 0

# 7  
Old 04-28-2011
Thank you for your answers and sorry for confusing you.

This is what I have for example:

Code:
15.657
47.234
178.99
472.88
982.45
3762.99
5933.21
9925.43
18772.22
53827.32
82611.21
104321.12

and this is what I need as output:

Code:
20
50
200
 500
 1000
 4000
 6000
 10000
 20000
 50000
 80000
 100000

Sorry again for confusing you, it's been a long day...

Thank you,
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. 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

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

5. UNIX for Dummies Questions & Answers

Rounding a decimal

Hi, I am currently using tcsh I am trying to round a decimal number to the ten-thousandths place For instance: 1.23456 is rounded up towards 1.2346 I am not looking for truncation, but for rounding. Anyone know how to do this with awk or expr? Thanks (2 Replies)
Discussion started by: miniwheats
2 Replies

6. Shell Programming and Scripting

Rounding Script Help

I need some help with my rouding script. I have started pretty much from scratch and have no idea if its correct or even close but I have been trying and have gotten to this point. i keep getting syntax errors and im not sure what is wrong. Here is what I got let value=$1; while do let... (4 Replies)
Discussion started by: kingrj46
4 Replies

7. Linux

Rounding Script Help

I need some help with my rouding script. I have started pretty much from scratch and have no idea if its correct or even close but I have been trying and have gotten to this point. i keep getting syntax errors and im not sure what is wrong. Here is what I got let value=$1; while do let... (0 Replies)
Discussion started by: kingrj46
0 Replies

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

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

10. Shell Programming and Scripting

Rounding off using BC.

Hello again. I'm trying to use BC to calculate some numbers in a shell script. I want to have the numbers rounded off to 1 decimal place. for example: initsize=1566720 zipsize=4733 I'm trying to get the ratio between them. the equation is: (($initsize-$zipsize)/$initsize)*100 so... (3 Replies)
Discussion started by: noodlesoup
3 Replies
Login or Register to Ask a Question