[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=$(( $(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 $?
	}

Output:

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

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

With rounding:

'{ print int ( (24*$1*60+60*$2+$3)*BYTES + 0.5 ) }'

or

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

The 'int' made already a good conversion from 2.29049e+08 to 229048709

I see no need/difference by adding + 0.5 .

Thank you

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

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.

In this case the suggested + 0.5 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.

From the point of mathematical "rounding" only my proposal makes sense.
Example:

echo 00 44 14.96 | awk '{ print int ( (24*$1*60+60*$2+$3)*BYTES + 0.5 ) }' BYTES=8627
22904340
echo 00 44 14.96 | awk '{ printf "%.f\n",(24*$1*60+60*$2+$3)*BYTES }' BYTES=8627
22904340
echo 00 44 14.96 | awk '{ print int ( (24*$1*60+60*$2+$3)*BYTES ) }' BYTES=8627
22904339

Out of sheer curiosity: bitrates usually are measured per second, and, by the conversion algorithm you apply, play time seems to be counted in minutes. For the correct calculation of the file size, shouldn't a factor of 60 be considered?

True, but PlayTime returns 3 numbers, hours ($1 -- 00) minutes ($2 -- 44) and seconds ($3 - 14.97).

But you got a point, i accidently used 24 instead of a 2nd 60 for the hours... :smiley: