Awk calculation rounding output to integers -


i have calculation , need it's output whole number, might return 1 desimals.

echo $((ram/50)) | awk '{printf( "%.0f\n", $1*0.$1 )}' 

so, ram/50, in case, returns 36, piped awk. want awk multiply number 0.the number itself, so: 36*0.36. return 12.96, want rounded 13. command now, 36*0.36 calculation doesn't work , printf removes .96, dosen't round off.

your problem related operator precedence. multiplication higher precedence string concatenation, need parentheses in order make work expect:

echo 36 | awk '{ printf "%.0f\n", $1 * ("0." $1) }' 

in version, order of operations means you're multiplying field 1 0, concatenating field 1, before passing result printf.


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -