Operating System - HP-UX
1753371 Members
5001 Online
108792 Solutions
New Discussion юеВ

Simple Arithmatic Function

 
SOLVED
Go to solution
Scott Frye_1
Super Advisor

Simple Arithmatic Function

I'm trying to do:

$i / $m = x
where i = 205
m = 400
and x should equal .5125. Problem is everything I've tried gives me x=0. What am I missing.
3 REPLIES 3
Patrick Wallek
Honored Contributor
Solution

Re: Simple Arithmatic Function

If this is in a script do:

x=$(echo "${i}/${m}" | bc -l)
A. Clay Stephenson
Acclaimed Contributor

Re: Simple Arithmatic Function

The problem is that the shell only does integer arithmetic. Use awk or bc.

#!/usr/bin/sh

I=205
M=400
RESULT=$(echo "scale=4; ${I} / ${M}" | bc)
echo "Result = ${RESULT}"

Man bc for details.

If it ain't broke, I can fix that.
Scott Frye_1
Super Advisor

Re: Simple Arithmatic Function

Works perfectly Patrick. Thanks!