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:
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:
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:
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:
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:
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.
printf "%.2f \n" $num
To provide multiple values:
num2=4.4444
printf "%.2f %.3f \n" $num1 $num2
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:
OR
echo num | awk '{printf("%.2f \n",$1)}'
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.