[awk] rounding a float number?

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [awk] rounding a float number?
# 1  
Old 03-25-2015
[awk] rounding a float number?


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=$(( $(pr_str) * 1024 / 8 ))
		t_TIMES=$( PlayTime | $SED s,":"," ",g)
		echo "$t_BYTERATE $t_TIMES"
		echo "$t_TIMES" | $AWK '{ (((24*$1*60)+(60*$2)+$3)*BYTES) }' BYTES=$t_BYTERATE
		return $?

86272 00 44 14.96

Sadly there is no longer any output at all when i remove the bold code line.

Expected output:
When i remove the bold code line, which is just there for debuging, i want to get the proper expected filesize.
As of now, i assume awk fails due to the underlined floating point number?

Playtime does the same as pr_str, just for the duration of the file.
$TMP.info is filled by another prior function.

Any ideas?
Thank you in advance

Last edited by sea; 03-25-2015 at 03:27 PM..
# 2  
Old 03-25-2015
The awk code will not produce output unless you print the result. Try:
echo "$t_TIMES" | $AWK '{ print (((24*$1*60)+(60*$2)+$3)*BYTES) }' BYTES=$t_BYTERATE

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 03-25-2015
With rounding:
'{ print int ( (24*$1*60+60*$2+$3)*BYTES + 0.5 ) }'

'{ printf "%.f\n", (24*$1*60+60*$2+$3)*BYTES }'

This User Gave Thanks to MadeInGermany For This Post:
# 4  
Old 03-25-2015
The 'int' made already a good conversion from 2.29049e+08 to 229048709

I see no need/difference by adding + 0.5.

Thank you
# 5  
Old 03-25-2015
The int() function truncates, rounds down. The value is 229048709.12 adding 0.5 yields 229048709.62 In both case the the int value is 229048709

For rounding you could try:
echo "$t_TIMES" | $AWK '{ printf "%.0f\n", (((24*$1*60)+(60*$2)+$3)*BYTES) }' BYTES=$t_BYTERATE

Last edited by Scrutinizer; 03-25-2015 at 04:51 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 03-25-2015
For rounding, I add 0.5 before truncating it. Decimal places less than 0.5000 will add up to less than 1, and be truncated, higher numbers will add one whole number then truncate.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 03-25-2015
In this case the suggested [ICODE]+ 0.5[/ICODE] was at the wrong place.
It was $3)*BYTES + 0.5 , where it should have been ($3+0.5).
Until now i was thinking that the + 0.5 was supposed for the +E conversion, which was already done by the int.

Makes perfectly sense now, thank you.

Last edited by sea; 03-25-2015 at 05:15 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

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

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

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

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 rounding up numbers

Hi, I have managed to round up numbers by using the following command: echo "5.54" | awk '{printf "%.0f\n", $1}' result 6 How can I round up all the numbers in a column in a file and print the lines with the new calculated totals? Thanks, (3 Replies)
Discussion started by: keenboy100
3 Replies

7. Shell Programming and Scripting

Float number format problem -Awk

Here is the script I'm using awk '{print $1,"\t",(($2+$3)/2)-x,"\t",(($2+$3)/2)+x,"\t",$4,"\t",$5}' x=500 $1 I just want to make float numbers (red) like normal numbers (green) output cX 1.65107e+08 1.65108e+08 13 64.2 cX 165112764 165113764 27 ... (7 Replies)
Discussion started by: ruby_sgp
7 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

comparing float with int / number

Hi all, I'm looking to modify a script to check disk space usage. Here is the code at the moment: # # The control file, MONITOR_DISK_SPACE, must be in the format ... Drive:;threshold_percentage # eg. # C:;95 # D:;98 # E:;90 # # For each line in the control file (MONITOR_DISK_SPACE)... (2 Replies)
Discussion started by: lelliott
2 Replies
Login or Register to Ask a Question

Featured Tech Videos