Table of Contents
1. Overview
In Bash scripting, dealing with numbers and specifically rounding them to a certain number of decimal places is a common task. For example, given a number like 3.14159, the goal is to round it to two decimal places, resulting in 3.14.
This article explores various methods to achieve this in Bash, focusing on rounding to two decimal places. We will compare each method’s performance and delve into each command’s details.
2. Using printf Command
The printf
command in Bash is similar to the printf
function in C. It is used to format the output.
Here is the syntax:
1 2 3 |
print options [format specifier] arguments |
Format specifier provides the format of output. In this case, we want to round the number to 2 decimal places, and that’s the reason we can put the format as "%.2f"
.
1 2 3 4 5 |
number=1.855678 rounded_number=$(printf "%.2f\n" $number) echo $rounded_number |
1 2 3 |
1.86 |
The explanation of the above code snippet is as follows:
%.2f
is format specifier here.
%
introduces format specifier..2
indicates that the number should be rounded to 2 decimal places.f
stands for floating-point number.
\n
is a newline character to ensure that output is followed by a new line.
$number
is a variable containing the number to be formatted.
printf
is fast and efficient for formatting numbers. It’s built into the shell and doesn’t require spawning a new process.
3. Using awk Command
The awk
is a text processing and data manipulation tool.. We can use the awk
command with format specifier as %.2f
as below:
1 2 3 4 |
number=1.855678 echo $number | awk '{printf("%.2f\n", $0)}' |
1 2 3 |
1.86 |
The explanation of the above code fence is as follows:
- The
echo $number
sends the number to the awk command. |
pipes output of echo to awk.- Inside awk,
{printf "%.2f\n", $0}
is action performed on the given input.printf "%.2f\n"
is similar to theprintf
command.$0
represents the entire line of the input, a number in this case.
4. Using bc
The bc
is an arbitrary precision calculator language. Let’s use bc
to round the number to 2 decimal places.
1 2 3 4 5 |
number=1.855678 rounded_number=$(echo "scale=2; $number/1"| bc) echo $rounded_number |
1 2 3 |
1.85 |
Please note that above bc command doesn’t round the decimal but truncates to 2 decimal places.
We can use the following command to do the round rather than truncate:
1 2 3 4 5 |
number=1.855678 rounded_number=$(echo "scale=2; ($number+0.005)/1"| bc) echo $rounded_number |
1 2 3 |
1.86 |
5. Using Python
Invoking Python for arithmetic operations provides high precision and is useful if the script interacts with Python or requires advanced mathematical functions.
1 2 3 4 |
number=1.855678 python -c "print(round(number, 2))" |
python -c
allows running a Python command as a string from the shell.print(round(number, 2))
calls Python’s round function, rounding the number to two decimal places.- The number in the Python command is the shell variable and needs to be passed correctly.
This is an excellent solution in case our script already interacts with Python. If not, it can cause extra overhead due to a call to the external interpreter.
6. Conclusion
In this article, we have discussed different ways to round to 2 decimal places.
printf
is fast and efficient for formatting numbers. It’s built into the shell and doesn’t require spawning a new process. This makes it an excellent choice for basic formatting.
bc
and awk
offer more precision but at the cost of performance. Python is powerful and precise but is the slowest option here due to the overhead of invoking an external interpreter.