0

Essentially, I am running a script that grabs a bunch of numbers from other files and puts them together in a useful way.

It currently works well, but it is printing every single digit from the numbers it is grabbing (grepping? is that a word?) and I would like to round it much earlier.

an example of the main idea is:

var1=$(grep.....)

echo $var1 >> output.dat

but this gives me something like 1.235215233462345345362 and I want many less digits.

Bonus question:

Some of the numbers returned are in scientific notation. Is there any way to change that as well?

ssh putty

Distributor ID: RedHatEnterpriseServer Description: Red Hat Enterprise Linux Server release 6.5 (Santiago) Release: 6.5 Codename: Santiago

Kusalananda
  • 333,661

1 Answers1

4

Use printf instead of echo :

$ echo "$a"
1.235215233462345345362

$ LC_ALL=C printf '%.1f\n' "$a"
1.2
$ LC_ALL=C printf '%.3f\n' "$a"
1.235

(LC_ALL=C is to make sure the decimal mark is always . (on input and output) regardless of the locale of the user (where it may be , instead)).