question about floating point calcuation with shell script / bc

Patrick Dung patrick_dkt at yahoo.com.hk
Mon Nov 12 09:19:35 PST 2007


Hello Peter

Thanks, it work.

Regards
Patrick

--- Peter Boosten <peter at boosten.org> wrote:

> On Mon, November 12, 2007 14:01, Patrick Dung wrote:
> > Hi
> >
> >
> > I have a file with numbers in each line.
> > Each number is a decimal number.
> > My task is to add them up and get the final answer.
> >
> >
> > I have searched with the search engine.
> > I found bash cannot handle floating point calculation.
> >
> >
> > I tried to use 'bc' and found if the final answer is < 1 (eg. 0.2)
> > It display .2 instead of 0.2 (no leading zero).
> >
> >
> > Any suggestion or other methods?
> > I know ksh could do floating point calculation
> > but I am now familiar with ksh.
> >
> 
> Try awk
> 
> awk '{sum += $1} END {printf "%.2f\n", sum}' file
> 
> assuming the file consists only of numbers in the first column.
> 
> Peter
> 
> 
> -- 
> http://www.boosten.org
> 
> 
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 


More information about the freebsd-questions mailing list