Bash is a well-known shell and command language used for performing tasks efficiently. While working in Bash scripting and explicitly playing with the floating numbers, it is often needed to round off the floating numbers. In this post, we will learn a few commands and techniques of Bash scripting to round off the number to 2 decimal places.

While displaying the numbers or variables that include float numbers in them, different commands can print the text or variables like echo, print, and printf. Although we can show the variables and numbers directly from such commands, however, the only command that has some extra features and capabilities is the printf command that concerns our goal.

printf Command

The printf command is similar to the printf() function in C language. It allows us to format and print the arguments.

Syntax

The syntax for writing the printf command in bash is:

printf “string” arguments

In the printf command, we first have to provide the string in inverted commas, and then we can give it the arguments.

Example

Let’s first print a simple text using the printf command:

printf “Hello from Linuxhint.”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/1-3.jpg" data-lazy- height="43" src="data:image/svg xml,” width=”470″>

You can see it has just printed out the string, but the username and hostname come right after the string.

For getting the string in a single line, we can use the newline character ‘n’ to have the clean and clear output:

printf “Hello from Linuxhint n

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/2-3.jpg" data-lazy- height="55" src="data:image/svg xml,” width=”500″>

Now we have a clean and clear output.

Now, let’s see how to provide the arguments to give this printf command a float number and round it off.

To provide the arguments, we need to use the specifier (%s) in the string which will be replaced by the arguments provided. For example:

printf “User number: %s n 24

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/3-3.jpg" data-lazy- height="59" src="data:image/svg xml,” width=”476″>

Alright, now you have understood the primary usage and functionality of the printf command. Let’s provide it a float number and see how to round off to 2 decimal places.

Precision Directive

Precision modifier is used for rounding off a float number.

The syntax to write a precision modifier is to give the number of decimal points you want to round off the number followed by the dot(.).

To round off a float number to 2 decimal places, you can execute the printf command as shown below:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/4-3.jpg" data-lazy- height="58" src="data:image/svg xml,” width=”417″>

You can see in the screenshot attached that the number “4.4444” is rounded off to “4.44”, as we desired it to be.

Similarly, instead of directly providing it a number, we can give a variable here as well.

num=4.4444

printf “%.2f n $num

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/5-4.jpg" data-lazy- height="330" src="data:image/svg xml,” width=”524″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/6-1.jpg" data-lazy- height="60" src="data:image/svg xml,” width=”391″>

To provide multiple values:

num1=3.333

num2=4.4444

printf “%.2f %.3f n $num1 $num2

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/7.jpg" data-lazy- height="332" src="data:image/svg xml,” width=”527″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/8.jpg" data-lazy- height="65" src="data:image/svg xml,” width=”385″>

If you still want to use the echo command and round off the number, in that case, you have to use the echo command with awk command to round off the number as shown below:

echo “3.333” | awk ‘{printf(“%.2f n”,$1)}’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/9.jpg" data-lazy- height="60" src="data:image/svg xml,” width=”656″>

OR

num=3.333

echo num | awk ‘{printf(“%.2f n”,$1)}’

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/10.jpg" data-lazy- height="326" src="data:image/svg xml,” width=”526″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/06/echo/11.jpg" data-lazy- height="63" src="data:image/svg xml,” width=”375″>

Conclusion

This post contains a detailed and profound guide on the printf command and we have learned to round off any number to 2 decimal places using the printf command. In addition, we have tried several examples to have sound knowledge on how to use the precision modifier to round off a number using the printf command. Moreover, we learned to round off any float number using the echo and awk command.