Is it possible to use BASH / bc / printf to round a float to it into the first significant digit?

I found tons and tons of information about rounding the float to a certain number of decimal places, using "scale" with BC and "% .xf" with printf, but if I work with numbers, t always has the same format as shown below, there is is there a way to round it to the first decimal place, which is not zero?

For example, let's say I have a list of numbers like this:

0.0008234535225
0.00547889294
0.000003243322

Is there a way to convert them to something like this:

0.0008
0.005
0.000003

Every Google result I came across says rounding to a certain number of digits instead of the first significant number, and I don't have much success in filtering them from search results, so he figured out this problem in itself is a little complicated.

- ?

+4
2

0:

grep -o "0.0*." <<< "$NUMBER"

:

grep -o "0.0*." <<< "0.000003243322"

0.000003
+4

, :

$ cat x
0.0008234535225
0.00547889294
0.000003243322
0.00012034

$ for line in $(<x); do
> exp="${line%%[^0.]*}" mant="${line#$exp}"
> echo "${exp}${mant:0:1}"
> done
0.0008
0.005
0.000003
0.0001
+1

All Articles