delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2015/11/26/09:19:21

X-Recipient: archive-cygwin AT delorie DOT com
DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:subject:references:to:from:message-id
:date:mime-version:in-reply-to:content-type
:content-transfer-encoding; q=dns; s=default; b=PkMy2we2W027Enql
ppaAdrdmfrTCNo4W2zp9rRisVEQ3B6qWzSVRnjX9aYvLmDoDKAp5dFG2dB6DAd7l
N+98PiwhWmT7mNa9zCPnyxlB9TE9DeOBucW4p7GhWBsIjdgittN7gcaYTcP4Alfm
f7I79iMdY1dC/ic4gl/aAqIA8Jw=
DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id
:list-unsubscribe:list-subscribe:list-archive:list-post
:list-help:sender:reply-to:subject:references:to:from:message-id
:date:mime-version:in-reply-to:content-type
:content-transfer-encoding; s=default; bh=3iL6ZExaM9XX+4aBV2o9wu
gqo6o=; b=YHR3OcP60z5nIYBq/mMd8rBSPKI60OMbZc5dzyDUwq+y1MOuWsBUfA
5OYWMDmKaPQy3IMPcO2hxfhz4Rps1v5ogf0cpS9nkCy/onHNeyZzefvWt/qdepki
JUYpkflL9L+oUW4KA+Sp85Vz9XsRULxbJ8r3l21haYrVwIO+WkQzA=
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com
Authentication-Results: sourceware.org; auth=none
X-Virus-Found: No
X-Spam-SWARE-Status: No, score=-0.7 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY,RP_MATCHES_RCVD autolearn=ham version=3.3.2
X-HELO: csmail.cs.umass.edu
Reply-To: moss AT cs DOT umass DOT edu
Subject: Re: Rounding off real (floating point) values - bash to awk
References: <CAE3taFBHyMhcGB=ux5xYVxcLM13G2JRpuTyDG-NtuSrp=eOhmQ AT mail DOT gmail DOT com>
To: cygwin AT cygwin DOT com
From: Eliot Moss <moss AT cs DOT umass DOT edu>
Message-ID: <565714CC.4070107@cs.umass.edu>
Date: Thu, 26 Nov 2015 09:18:52 -0500
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0
MIME-Version: 1.0
In-Reply-To: <CAE3taFBHyMhcGB=ux5xYVxcLM13G2JRpuTyDG-NtuSrp=eOhmQ@mail.gmail.com>
X-IsSubscribed: yes

On 11/26/2015 8:24 AM, Lester Anderson wrote:
> Hello,
>
> I can use a script like:
>
> #!/bin/bash
> x=3.7
> # pass variable x to awk via -v (var=value)
> awk -v x=$x 'BEGIN { printf "%3.0f\n", x }'
> #
>
> which returns the value 4 as expected, but are there any other methods
> that can be used?

In bash this must be a string (bash uses only fixed width integers for numbers),
so you can put as many decimal places as you like.  awk will treat it as a string
or floating point number, depending on context.  The f output format forces conversion.
Another way is to do arithmetic;  even x+0 will do it.  IIRC, all numbers in awk are
doubles (IEEE 64-bit floats).  The documentation on awk can tell you more about
conversions, rounding, etc.

Regards -- Eliot Moss

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019