How to get bc (1) to print a leading zero?

In the Makefile, I am doing something like the following:

echo "0.1 + 0.1" | bc 

(in the real file, the numbers, of course, are dynamic)

He prints .2 , but I want him to print 0.2 .

I would like to do this without resorting to sed , but I cannot find how to get bc to print zero. Or is bc simply unable to do this?

+45
unix bash bc
Dec 6 2018-11-12T00:
source share
10 answers

You can also resort to awk for formatting:

  echo "0.1 + 0.1" | bc | awk '{printf "%f", $0}' 

or with awk itself doing the math:

  echo "0.1 0.1" | awk '{printf "%f", $1 + $2}' 
+26
Dec 06 '11 at 15:38
source share

This might work for you:

 echo "x=0.1 + 0.1; if(x<1) print 0; x" | bc 
+30
Dec 06 '11 at 17:50
source share

After a quick look at the source (see bc_out_num() , line 1461), I see no obvious way to make the leading 0 print if the integer part is 0 . If I did not miss anything, this behavior does not depend on a parameter that can be changed using the command line flag.

Short answer: no, I don’t think there is a way to make bc print numbers the way you want.

I don't see anything wrong with using sed if you still want to use bc . The following does not look so terrible, IMHO:

 [me@home]$ echo "0.1 + 0.1" | bc | sed 's/^\./0./' 0.2 

If you really want to avoid sed , eljunior's and choroba's suggestions are pretty neat, but they require a cost- specific setup to avoid trailing zeros. This may or may not be a problem for you.

+18
Dec 06 '11 at 16:19
source share

I can not find anything about the output format in the documentation. Instead of sed, you can also use for printf:

 printf '%3.1f\n' $(bc<<<0.1+0.1) 
+5
Dec 06 2018-11-11T00:
source share
 $ bc -l <<< 'x=-1/2; if (length (x) == scale (x) && x != 0) { if (x < 0) print "-",0,-x else print 0,x } else print x' 

This is pure bc . It defines a leading zero by comparing the result of length with the scale expression. It works with both positive and negative numbers.

+3
Jul 30 '15 at 23:32
source share

This will also handle negative numbers:

 echo "0.1 - 0.3" | bc | sed -r 's/^(-?)\./\10./' 
+2
Mar 14 '14 at 17:11
source share

this uses only bc and works with negative numbers:

 bc <<< "x=-.1; if(x==0) print \"0.0\" else if(x>0 && x<1) print 0,x else if(x>-1 && x<0) print \"-0\",-x else print x"; 

try:

 for y in "0" "0.1" "-0.1" "1.1" "-1.1"; do bc <<< "x=$y; if(x==0) print \"0.0\" else if(x>0 && x<1) print 0,x else if(x>-1 && x<0) print \"-0\",-x else print x"; echo; done 
+1
Jun 09 '13 at 6:25
source share

Perhaps bc is actually not the best bench calculator for the modern era. Other languages ​​will give you more control. Here are working examples that print values ​​in the range (-1.0 .. + 1.0) with an initial zero. These examples use bc , AWK and Python 3 .

 #!/bin/bash echo "using bc" time for (( i=-2; i<=+2; i++ )) { echo $(bc<<<"scale=1; x=$i/2; if (x==0||x<=-1||x>=1) { print x } else { if (x<0) { print \"-0\";-x } else { print \"0\";x } } ") } echo echo "using awk" time for (( i=-2; i<=+2; i++ )) { echo $(echo|awk "{printf \"%.1f\",$i/2}") } echo echo "using Python" time for (( i=-2; i<=+2; i++ )) { echo $(python3<<<"print($i/2)") } 

Note that the Python version is about 10 times slower if that matters.

+1
Apr 20 '15 at 22:05
source share

Based on potongs answer ,

For fractional results:

 echo "x=0.1 + 0.1; if(x<1 && x > 0) print 0; x" | bc -l 

Please note that negative results are not displayed correctly. For this, Aquarius Power has a solution .

0
Mar 14 '17 at 12:59 on
source share

echo "$a / $b" | bc -l | sed -e 's/^-\./-0./' -e 's/^\./0./'

This should work for all cases where the results are:

  • "-. 123"
  • ". 123"
  • "- 1.23"
  • "1.23"

Explanation:

  • For everything that starts only with -. , replace -. by -0.

  • For everything that starts only with . replace . by 0.

0
Nov 03 '17 at 4:39 on
source share



All Articles