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:date:from:to:subject:message-id:references :mime-version:content-type:in-reply-to; q=dns; s=default; b=yLNN 2gT55xnNdf3XtuFaLyBgUfb6fRo17++S5mW5BgFmzVkObZOLm5H3rvnykvDiQWL1 fq3nid04Z4iqLPyBDS7e3/2ZNIYUX4etes+fDnp4HZDm+VmwS4cNh8j0kWJ/0dOp P7/TU+Qp0S2EeUFAlzXfYGu63rNzOQhqgQNxkMQ= 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:date:from:to:subject:message-id:references :mime-version:content-type:in-reply-to; s=default; bh=nw4//Tk2QP RXKbQP4iYAEz2UkUM=; b=ustwu+S+C504JBUM+5cXQpzf0SssPipt9KMvU8HWTk 8sYND6n27ffSX8+pm+Phw6pW7XIi7k51AcTIJFebKxs+VhfGHavNCRK5koFx1QdL w9mx1KYbFW9Jtjz1weZYb6E///xBKV4otjL9bdoFlakMdimI9HRR/lvSXGHUctua E= Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , 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.1 required=5.0 tests=AWL,BAYES_05,KAM_LAZY_DOMAIN_SECURITY,RCVD_IN_DNSWL_LOW autolearn=no version=3.3.2 X-HELO: mail108.syd.optusnet.com.au Date: Thu, 3 Dec 2015 20:13:59 +1100 From: Duncan Roe To: cygwin AT cygwin DOT com Subject: Re: Rounding off real (floating point) values - bash to awk Message-ID: <20151203091359.GA2760@dimstar.local.net> Mail-Followup-To: cygwin AT cygwin DOT com References: <565714CC DOT 4070107 AT cs DOT umass DOT edu> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <565714CC.4070107@cs.umass.edu> User-Agent: Mutt/1.5.22 (2013-10-16) X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=R4L+YolX c=1 sm=1 tr=0 a=+cDhJlqnNvz9E7c5l3ERGg==:117 a=+cDhJlqnNvz9E7c5l3ERGg==:17 a=y26AOypDAAAA:8 a=PO7r1zJSAAAA:8 a=iaFubHWvAAAA:8 a=kj9zAlcOel0A:10 a=wUQvQvOEmiQA:10 a=ZjU40tYuB5xho_q9ZDIA:9 a=CjuIK1q_8ugA:10 X-IsSubscribed: yes On Thu, Nov 26, 2015 at 09:18:52AM -0500, Eliot Moss wrote: > 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 > You can skip using awk: under bash 'printf "%3.0f\n" 3.7' gives " 4". Or 'printf "%.0f\n" 3.7' gives 4, Cheers ... Duncan. -- 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