10

In Ubuntu 14.04.1 LTS 64-bit bash I am declearing floating point variables by multiplying floating point bash variables in bc with scale set to 3; however, I cannot get the number of digits after the decimal point to be zero and get rid of the zero to the left of the decimal point. How can I transform, say 0.005000000 into .005? This is necessary due to my file naming convention. Thanks for your recommendations.

UPDATE: Can I use it for already defined shell variables and redefining them? The following code gives me an error.

~/Desktop/MEEP$ printf "%.3f\n" $w
bash: printf: 0.005000: invalid number
0,000

The output of locale

@vesnog:~$ locale
LANG=en_US.UTF-8
LANGUAGE=en_US
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=tr_TR.UTF-8
LC_TIME=tr_TR.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=tr_TR.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=tr_TR.UTF-8
LC_NAME=tr_TR.UTF-8
LC_ADDRESS=tr_TR.UTF-8
LC_TELEPHONE=tr_TR.UTF-8
LC_MEASUREMENT=tr_TR.UTF-8
LC_IDENTIFICATION=tr_TR.UTF-8
LC_ALL=

The output of echo $w

@vesnog:~$ echo $w
0.005000
0

5 Answers 5

12

A simple way is to use printf:

$ printf "%.3f\n" 0.005000000000
0.005

To remove the leading 0, just parse it out with sed:

$ printf "%.3f\n" 0.005000000000 | sed 's/^0//'
.005
6
  • 1
    Thanks for the answer, can I use the same setup with an already assigned variable like I described in the edit of my OP?
    – Vesnog
    Dec 24, 2014 at 11:03
  • I think I am having difficulties with the delimiter for the decimal point, is there an easy way to set it? It even says invalid number in the examples you provided in this post.
    – Vesnog
    Dec 24, 2014 at 11:08
  • 1
    @Vesnog please edit your question and include your locale. It looks like you're using a locale with comma as a decimal separator instead of dot.
    – terdon
    Dec 24, 2014 at 11:15
  • Where can I find this file?
    – Vesnog
    Dec 24, 2014 at 11:15
  • I'm not at my computer, so I can't check now, but it should be in the output of locale. Also show us the output of echo $w.
    – terdon
    Dec 24, 2014 at 11:17
11

There is a special variable called scale. You can set this variable to limit the precision.

EXAMPLE

$ echo "300/7" | bc -l
42.85714285714285714285

To limit the precision,

$ echo "scale=2; 300/7" | bc -l
42.85

UPDATED

$ echo "scale=3; 300/7" | bc -l | sed 's/[0-9]*\././g'
.857
1
  • I up-vote because this is the answer to the question that I googled.
    – Ben Barkay
    Sep 4, 2019 at 17:58
0

This sed command removes trailing zeros:

sed 's/\(0\.0*[1-9][1-9]*\)0*/\1/'

It remembers any values beginning with 0., followed by zero or more 0's, followed by 1 or more digits from 1-9, followed by zero or more 0's. It then truncates the zero or more trailing 0's.

This allows you to parse out an arbitrary number of trailing zeros, and retain any arbitrarily low number greater than zero. regardless of how small it is.

For example

0.054000 becomes 0.054, or 0.00000010000000 becomes 0.0000001, and so on.

For more info check out Grymoires awesome sed tutorial.

0

I know I'm late but for any new people who google this problem. Invalid number hasn't been solved from the OP in the most voted answer, in fact the answer also gives me an invalid number error.

Reason is LC_NUMERIC variable from locale. Simple solution: use "," instead of "." as a decimal point. Also simple, change the locale variable.

Test it

export LC_NUMERIC="en_US.UTF-8"

Permanent

echo 'export LC_NUMERIC="en_US.UTF-8"' >>~/.bashrc
1
  • Good point for calling out the locale issue but note that not every system has a en_US.UTF-8 locale, every system has a C locale though (where the numerical radix is also .; and more generally a lot of the locale convention align with the US-english ones). Also note that LC_ALL if set would take precedence over LC_NUMERIC. See also What does "LC_ALL=C" do? Feb 3, 2022 at 18:46
0

Why not grep?

echo "$your_number" | grep -o '.*[1-9]'
1
  • The question is a little unclear,  but it seems that the OP wants to chop a number to three decimal places, not remove trailing zeroes,  so 0.123456000 should become 0.123 (not 0.123456), and 1.500000000 should become 1.500 (not 1.5). Sep 4, 2022 at 7:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .