Is there a way to do decimal calculations in Bash?

Is there a way to do decimal calculations in Bash?

Whilst we can trick it into calculating (but not generating) a decimal output by multiplying the numbers by for example a factor of 1000 and then doing an text based splitting, this is a ugly workaround and creates complex code. There is however a utility in Bash which can natively do decimal based calculations without any tricks or workarounds!

Is it possible to get a decimal output from doing?

Closed 5 years ago. Basically in Bash, what I’m trying to do is take a input of seconds from a user and find the hours. So basically if the user enter 35 the output should be 0.00972222. But bash gives me is a zero. heres the command I have: Is there a way I can make it so it prints out 0.00972222 if I enter 35. Thanks!

What should the output of Bash be if the user enters 35?

So basically if the user enter 35 the output should be 0.00972222. But bash gives me is a zero. heres the command I have: Is there a way I can make it so it prints out 0.00972222 if I enter 35. Thanks! To control the number of significant digits printed, use scale=N. For example:

What’s the difference between printf and BC in Bash?

There’s another difference between printf and bc (or dc ): printf rounds to the nearest number to two decimal places, whereas bc (or dc) rounds correct to two decimal places. If you want this latter behavior and assign to a variable T the result, you can use, e.g.,

How to compare decimal points in shell script?

I want to compare decimal points in shell script. You do not need to use awk and grep in same line, awk does it all. Do not use old and outdated back-tics, use parentheses. So bc -l expression will print 1 for success and 0 for failure.

How to print decimal numbers in shell script?

Do not use old and outdated back-tics, use parentheses. So bc -l expression will print 1 for success and 0 for failure. Here’s how to put this into a Posix shell function which can thereafter be easily used in shell tests (presumably bc reserves return codes for error codes):

Is there a syntax error for decimal points in Bash?

Although his reply is correct for decimal points, bash throws (standard_in) 1: syntax error with floating point arithmetic. This returns incorrect output with a warning although with an exit code of 0.