bc truncate floating point number bc truncate floating point number bash bash

bc truncate floating point number


Use / operator.

echo '(4.2-1.3) / 1' | bc


Dividing by 1 works ok if scale is 0 (eg, if you start bc with bc and don't change scale) but fails if scale is positive (eg, if you start bc with bc -l or increase scale). (See transcript below.) For a general solution, use a trunc function like the following:
define trunc(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x }

Transcript that illustrates how divide by 1 by itself fails in the bc -l case, but how trunc function works ok at truncating toward zero:

> bc -lbc 1.06.95[etc...]for (x=-4; x<4; x+=l(2)) { print x,"\t",x/1,"\n"}-4  -4.00000000000000000000-3.30685281944005469059 -3.30685281944005469059-2.61370563888010938118 -2.61370563888010938118-1.92055845832016407177 -1.92055845832016407177-1.22741127776021876236 -1.22741127776021876236-.53426409720027345295  -.53426409720027345295.15888308335967185646   .15888308335967185646.85203026391961716587   .852030263919617165871.54517744447956247528  1.545177444479562475282.23832462503950778469  2.238324625039507784692.93147180559945309410  2.931471805599453094103.62461898615939840351  3.62461898615939840351define trunc(x) { auto s; s=scale; scale=0; x=x/1; scale=s; return x }for (x=-4; x<4; x+=l(2)) { print x,"\t",trunc(x),"\n"}-4  -4-3.30685281944005469059 -3-2.61370563888010938118 -2-1.92055845832016407177 -1-1.22741127776021876236 -1-.53426409720027345295  0.15888308335967185646   0.85203026391961716587   01.54517744447956247528  12.23832462503950778469  22.93147180559945309410  23.62461898615939840351  3


Try the following solution. It will truncate anything after the decimal point without a problem:

echo 'x = 4.2 - 1.3; scale = 0; x / 1' | bc -lecho 'x = l(101) / l(10); scale = 0; x / 1' | bc -l

You can make the code a tad shorter by performing calculations directly on the numbers:

echo 'scale = 0; (4.2 - 1.3) / 1' | bc -lecho 'scale = 0; (l(101) / l(10)) / 1' | bc -l

In general, you can use this function to get only the integer part of a number:

define int(x) {    auto s;    s = scale;    scale = 0;    x /= 1; /* This will have the effect of truncating x to its integer value */    scale = s;    return (x);}

Save that code into a file (let's call it int.bc) and run the following command:

echo 'int(4.2 - 1.3);' | bc -l int.bc