30.6 C
Jaipur
Monday, June 21, 2021

How to Round to 2 Decimal Places in Bash – Linux Hint

Must read

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.”

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

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

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.

num=4.4444

printf “%.2f n $num

To provide multiple values:

num1=3.333

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:

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

OR

num=3.333

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.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article