1833847 Members
2310 Online
110063 Solutions
New Discussion

scripting question

 
SOLVED
Go to solution
Charles McCary
Valued Contributor

scripting question

Group,

Anyway to add two decimal numbers of differing precision in a ksh script?

tx,

C
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: scripting question

Hi Charles:

Try this:

echo "1.11 + 3.3"|bc

...JRF...
Abel Berger
Regular Advisor

Re: scripting question

For example :

set typ -i x
x=`echo 100.99 + 200.88 | bc`
echo $x

Abel Berger
Regular Advisor

Re: scripting question

For example :

set type -i x
x=`echo 100.99 + 200.88 | bc`
echo $x

Johannes Falk
New Member

Re: scripting question

Oops,
set type -i x
is nonsense! This sets $1 to "type", $2 to "-i" and $3 to "x"

But a typeset -i x
would set $x to integer, and this is not usefull here, because the
result is a float.

So simply use:
$ x=$(echo "1.11+3.3"|bc); echo "$x"
4.41
is ok

By the way, mind the precision in bc:
$ echo 'scale=6; 10/3' | bc
3.333333
$ echo 'scale=3; 10/3' | bc
3.333
$ echo '10/3' | bc
3

Hanno
Moin, moin!
Abel Berger
Regular Advisor

Re: scripting question

Hi Falk,

I MADE THE TEST AND ITS FUNCTIONED.
I WOULD LIKE THAT YOU MADE TOO.
THEREFORE WHEN YOU USE BC COMMAND THE
INTEGER VARIABLE BECOMES FLOATING,

OK.





Johannes Falk
New Member

Re: scripting question

Hi Abel,

I didn't understand your last question but I tested this:
c13:/tmp$ x=$(echo "1.11+3.3"|bc); echo "$x"
4.41
c13:/tmp$ typeset -i x
c13:/tmp$ x=$(echo "1.11+3.3"|bc); echo "$x"
4

So x remains an int if it is set so,
and the result becomes truncated.
Moin, moin!
Abel Berger
Regular Advisor

Re: scripting question

Hi Falk,

The problem among my test and your test is
the ` `. Your test use () and $ therefore the
results are different.
Test makes equal mine is above and looks at the result. OK !

Regards...
Abel Berger