From owner-freebsd-questions@FreeBSD.ORG Mon Nov 12 17:19:35 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 6F65A16A418 for ; Mon, 12 Nov 2007 17:19:35 +0000 (UTC) (envelope-from patrick_dkt@yahoo.com.hk) Received: from web54306.mail.re2.yahoo.com (web54306.mail.re2.yahoo.com [206.190.49.116]) by mx1.freebsd.org (Postfix) with SMTP id 07F5E13C4A7 for ; Mon, 12 Nov 2007 17:19:34 +0000 (UTC) (envelope-from patrick_dkt@yahoo.com.hk) Received: (qmail 33554 invoked by uid 60001); 12 Nov 2007 17:19:26 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com.hk; h=X-YMail-OSG:Received:Date:From:Subject:To:Cc:In-Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-ID; b=MKH3iHwX80IGR8GLson9I3JzzbgRR18fUgnkzqNgh/JrwHxvgQGebq2Z/SxEb73hapqRuORAWde6FgRe7DytADdpD5fpFgLsaHbvgvdHNN0G912caEegSP2aTWtErq3NIu4zT1sNE7vbJNzFgEnUAbIAqMcIQCRE2Lz0nFfdRvs=; X-YMail-OSG: aMqYHHAVM1mSY5W6Fm.gvCCdkm7nOHly4fQIX0rpqaMH.FV_lwH_LC_ygtll1XSliCgdv5qHhbtfqz9SCNgE7qTpzyRqYW8eIHOoK7x31o5lLklAoxvAzwKypU7sfzWiwjtZl4riTEjHuckKkb_8swG2Bw-- Received: from [61.18.170.54] by web54306.mail.re2.yahoo.com via HTTP; Mon, 12 Nov 2007 09:19:26 PST Date: Mon, 12 Nov 2007 09:19:26 -0800 (PST) From: Patrick Dung To: Peter Boosten In-Reply-To: <63041.212.159.200.167.1194875048.squirrel@www.boosten.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Message-ID: <930599.33100.qm@web54306.mail.re2.yahoo.com> 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 17:19:35 -0000 Hello Peter Thanks, it work. Regards Patrick --- Peter Boosten 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