From: Eliot Moss <moss@cs.umass.edu>
To: cygwin@cygwin.com
Subject: Re: Rounding off real (floating point) values - bash to awk
Date: Thu, 26 Nov 2015 15:32:00 -0000 [thread overview]
Message-ID: <565714CC.4070107@cs.umass.edu> (raw)
In-Reply-To: <CAE3taFBHyMhcGB=ux5xYVxcLM13G2JRpuTyDG-NtuSrp=eOhmQ@mail.gmail.com>
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
next prev parent reply other threads:[~2015-11-26 14:19 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 [this message]
2015-12-03 9:14 ` Duncan Roe
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=565714CC.4070107@cs.umass.edu \
--to=moss@cs.umass.edu \
--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).