Technology and General Blog

Bash is a properly-regarded shell and command language used for accomplishing jobs efficiently. When doing the job in Bash scripting and explicitly enjoying with the floating quantities, it is generally wanted to round off the floating figures. In this submit, we will find out a number of commands and strategies of Bash scripting to spherical off the amount to 2 decimal locations.

Whilst displaying the numbers or variables that include things like float figures in them, different instructions can print the text or variables like echo, print, and printf. Though we can demonstrate the variables and quantities instantly from these commands, nevertheless, the only command that has some extra functions and abilities is the printf command that problems our intention.

printf Command

The printf command is related to the printf() operate in C language. It permits us to format and print the arguments.


The syntax for producing the printf command in bash is:

printf “string” arguments

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

Case in point

Let us to start with print a uncomplicated textual content using the printf command:

printf “Howdy from Linuxhint.”

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

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

printf “Hi from Linuxhint n

Now we have a thoroughly clean and apparent output.

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

To deliver the arguments, we will need to use the specifier (%s) in the string which will be replaced by the arguments offered. For illustration:

printf “Person variety: %s n 24

Alright, now you have understood the most important use and features of the printf command. Let’s provide it a float selection and see how to round off to 2 decimal places.

Precision Directive

Precision modifier is used for rounding off a float amount.

The syntax to produce a precision modifier is to give the number of decimal points you want to spherical off the quantity adopted by the dot(.).

To round off a float amount to 2 decimal places, you can execute the printf command as proven down below:

You can see in the screenshot hooked up that the quantity “4.4444” is rounded off to “4.44”, as we ideal it to be.

In the same way, as a substitute of immediately supplying it a quantity, we can give a variable here as very well.


printf “%.2f n $num

To supply multiple values:



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

If you even now want to use the echo command and round off the amount, in that circumstance, you have to use the echo command with awk command to spherical off the quantity as revealed below:

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



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


This put up includes a thorough and profound guide on the printf command and we have realized to spherical off any range to 2 decimal places using the printf command. In addition, we have tried a number of examples to have seem understanding on how to use the precision modifier to round off a range using the printf command. Furthermore, we uncovered to spherical off any float amount employing the echo and awk command.

Leave a Reply

Your email address will not be published. Required fields are marked *