1832902 Members
2489 Online
110048 Solutions
New Discussion

shell script

 
Jeff Hoevenaar_1
Occasional Contributor

shell script

I am trying to get a decimal output from the following.


let a=50/100
echo $a

output is 0
how do I get a 0.50 output?
10 REPLIES 10
Paul Hite_2
Frequent Advisor

Re: shell script

Try:

a=`echo "scale=2 ; 50 / 100" | bc`
echo $a
Klaus  Frank
Frequent Advisor

Re: shell script

Hi

try formated output with

printf "%5.2f" $a

see man printf for detailed information

best regards
Klaus Frank
... we all can make it with a little help ...
Brian M. Fisher
Honored Contributor

Re: shell script

Hoovey,

Try the following:
X=$(print|awk '{printf("%.2fn",50/100)}')

This may look ugly, but the awk command is capable of processing floating-point numbers.

Details of the command:
$() - execute the shell commands within
awk - awk command
print - pass awk blank line
printf - formatted print command
%.2f - real number 2 places past decimal
n - newline

Brian
<*(((>< er

Perception IS Reality
Emmanuel Eyer
Frequent Advisor

Re: shell script

Shell makes only integer calculation. Use bc if you need floating point (see previous answer) is a good idea, since bc is quite universal. However, there might be situations where you do not want to (or you cannot) rely on external programs.

It is possible, though a bit complicated, to do it all in Shell (Posix/Korn, but may work in Bourne too) using a function. I wrote such a function (called fdiv), put in the attachment. For example:

$ fdiv 3 7 3
0.428

(the 3rd parameter is the number of digits).

Hope it helps,
Emmanuel
curt larson
Frequent Advisor

Re: shell script

you could do this

#! /usr/dt/bin/dtksh

float a # float is usually aliased to typeset -E

(( a = 50/100 ))
printf "%fn" $a
nobody else has this problem
curt larson
Frequent Advisor

Re: shell script

looks like the back slash didn't show up in the printf format statement.

printf "fn" $a

of course the printf supports the usual C syntax of:
%[flags][field_width][precision][base]conversion
nobody else has this problem
curt larson
Frequent Advisor

Re: shell script

I dont' why the percent of the backslash isn't showing up. So I'll put the format into a word format and hope you can understand that better;

printf "(percent)f(backslash)n" $a
nobody else has this problem
CHRIS_ANORUO
Honored Contributor

Re: shell script

Curt, use (double slash) //
It is a thing with the forum postin, for front slash to appear, you have to make it double too.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
CHRIS_ANORUO
Honored Contributor

Re: shell script

bc -l will give you decimal points in you calculations.
When We Seek To Discover The Best In Others, We Somehow Bring Out The Best In Ourselves.
George A Bodnar
Trusted Contributor

Re: shell script

Do the following to vary your formula and get results in decimal:

# Set scale = # of decimal places you want
NUMBER="10/5" # Your formula
ANSWER=`bc -l <<-EOF
scale = 2
${NUMBER}
EOF`

This should work for you, but the value in ANSWER is text, not number since shell can
only handle integer values.