Thursday, January 22, 2009

Rounding Numbers

Need to be able to round numbers in bash to the nearest whole number? You can use a single line of code in AWK to do the job. For example, the following code will make the number 29.498398 round up to 29.50:

$ echo 29.498398 | awk '{ rounded = sprintf("%.2f", $1); print rounded }'
29.50

No comments: