public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Duncan Roe <duncan_roe@acslink.net.au>
To: cygwin@cygwin.com
Subject: Re: Rounding off real (floating point) values - bash to awk
Date: Thu, 03 Dec 2015 09:14:00 -0000	[thread overview]
Message-ID: <20151203091359.GA2760@dimstar.local.net> (raw)
In-Reply-To: <565714CC.4070107@cs.umass.edu>

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

  reply	other threads:[~2015-12-03  9:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-26 13:30 Lester Anderson
2015-11-26 15:32 ` Eliot Moss
2015-12-03  9:14   ` Duncan Roe [this message]
2015-11-26 16:33 ` Steven Penny
2015-11-26 17:20   ` Steven Penny
2015-11-26 17:27     ` Helmut Karlowski
2015-11-26 18:04 ` Achim Gratz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20151203091359.GA2760@dimstar.local.net \
    --to=duncan_roe@acslink.net.au \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).