This is the mail archive of the
cygwin
mailing list for the Cygwin project.
Re: Rounding off real (floating point) values - bash to awk
- From: Duncan Roe <duncan_roe at acslink dot net dot au>
- To: cygwin at cygwin dot com
- Date: Thu, 3 Dec 2015 20:13:59 +1100
- Subject: Re: Rounding off real (floating point) values - bash to awk
- Authentication-results: sourceware.org; auth=none
- References: <CAE3taFBHyMhcGB=ux5xYVxcLM13G2JRpuTyDG-NtuSrp=eOhmQ at mail dot gmail dot com> <565714CC dot 4070107 at cs dot umass dot 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