From owner-freebsd-questions@FreeBSD.ORG Mon Nov 12 13:44:21 2007 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 4DBDF16A41A for ; Mon, 12 Nov 2007 13:44:21 +0000 (UTC) (envelope-from peter@boosten.org) Received: from smtpq2.tilbu1.nb.home.nl (smtpq2.tilbu1.nb.home.nl [213.51.146.201]) by mx1.freebsd.org (Postfix) with ESMTP id 0E23913C49D for ; Mon, 12 Nov 2007 13:44:20 +0000 (UTC) (envelope-from peter@boosten.org) Received: from [213.51.146.188] (port=48039 helo=smtp3.tilbu1.nb.home.nl) by smtpq2.tilbu1.nb.home.nl with esmtp (Exim 4.30) id 1IrZaF-00081x-9C; Mon, 12 Nov 2007 14:44:11 +0100 Received: from cp268254-a.landg1.lb.home.nl ([213.51.103.48]:4917 helo=ra.egypt.nl) by smtp3.tilbu1.nb.home.nl with esmtp (Exim 4.60) (envelope-from ) id 1IrZaD-0007zT-FS; Mon, 12 Nov 2007 14:44:11 +0100 Received: from www.boosten.org (localhost.egypt.nl [127.0.0.1]) by ra.egypt.nl (Postfix) with ESMTP id D2917398B6; Mon, 12 Nov 2007 14:44:08 +0100 (CET) Received: from 212.159.200.167 (proxying for 172.21.129.43) (SquirrelMail authenticated user peter) by www.boosten.org with HTTP; Mon, 12 Nov 2007 14:44:08 +0100 (CET) Message-ID: <63041.212.159.200.167.1194875048.squirrel@www.boosten.org> Date: Mon, 12 Nov 2007 14:44:08 +0100 (CET) From: "Peter Boosten" To: "Patrick Dung" User-Agent: SquirrelMail/1.5.1 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Spam-Score: -0.0 (/) Cc: FreeBSD Questions Subject: Re: question about floating point calcuation with shell script / bc X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Nov 2007 13:44:21 -0000 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 +=3D $1} END {printf "%.2f\n", sum}' file assuming the file consists only of numbers in the first column. Peter --=20 http://www.boosten.org