I currently use the following to compute the following at the bash command line with commas every three digits:
printf "%'3.f\n" $(((29246636)*4096))
119,794,221,056
The equation needs to start with $((
and also end with ))
. The Linux Command Line reference has more information about formatting text.
I also have that:
printf "%'.3f \n" $(((31200310)*4096))
127,796,469,760.000
I want to make sure that when there is a division that there is no fractional part left over.
Is the best way to accomplish this as:
printf "%'.3f\n" $(((((125046784)*512)/1024)/64))
976,928.000
But this is problematic. The last three digits always appear to be .000 and fractional parts do not seem to be recorded after the decimal point. It is fine as long as the division is guaranteed to be even with no fractional leftover. But if that is not the case, the answer seem to be only the decimal part with zeros to the right of the decimal point and no indication that there is a fractional part.
I understand that this question seems simple. But for now I end up using the Linux Ubuntu calculator instead of the simpler command line because of this potential trap.
Thank you all for any insight to make this calculation with division and a fractional remainder easier on the command line. And, yes, I did try 64.0 instead of 64 at which point I get a syntax error. Perhaps something more sophisticated needs to be used.
I found this reference: "How to print the first three digits after the decimal point of a floating number?"
But I still do not understand how this might be applied in this case to get the desired result. Any help would be appreciated.
After reading the suggestions, I came up with the following bash script that I store in /usr/local/sbin/quotrem so that I can access it from any command prompt. This script is lacking still because I have to download it each time I want to use it on a different Ubuntu machine. Plus, it is not a desktop icon that I can just double click to input the required values and get the answer. Perhaps others can make suggestions how to improve there or in another way.
#!/bin/bash
#This quoterm script takes two arguments: quoterm a b
#Both a and b are integers and the result is a / b with a remainder of a % b
a=$1;
b=$2;
printf "a=%'3.f " $((a))
printf "b=%'3.f " $((b))
printf "a/b = %'3.f " $((a / b))
printf "with the remainder of %'3.f " $((a % b))
printf "\n"
Note that the use of /usr/local/sbin, I found it from this link: 3.16. /sbin : System binaries. I also found the scripting information from this link: The Linux Command Line: 32 – Positional Parameters - Accessing The Command Line