public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Rounding off real (floating point) values - bash to awk
@ 2015-11-26 13:30 Lester Anderson
  2015-11-26 15:32 ` Eliot Moss
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Lester Anderson @ 2015-11-26 13:30 UTC (permalink / raw)
  To: cygwin

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?

--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounding off real (floating point) values - bash to awk
  2015-11-26 13:30 Rounding off real (floating point) values - bash to awk Lester Anderson
@ 2015-11-26 15:32 ` Eliot Moss
  2015-12-03  9:14   ` Duncan Roe
  2015-11-26 16:33 ` Steven Penny
  2015-11-26 18:04 ` Achim Gratz
  2 siblings, 1 reply; 7+ messages in thread
From: Eliot Moss @ 2015-11-26 15:32 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounding off real (floating point) values - bash to awk
  2015-11-26 13:30 Rounding off real (floating point) values - bash to awk Lester Anderson
  2015-11-26 15:32 ` Eliot Moss
@ 2015-11-26 16:33 ` Steven Penny
  2015-11-26 17:20   ` Steven Penny
  2015-11-26 18:04 ` Achim Gratz
  2 siblings, 1 reply; 7+ messages in thread
From: Steven Penny @ 2015-11-26 16:33 UTC (permalink / raw)
  To: cygwin

On Thu, Nov 26, 2015 at 7:24 AM, Lester Anderson wrote:
> x=3.7
> 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?

echo 3.7 | awk '{printf "%.0f", $0}'

--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounding off real (floating point) values - bash to awk
  2015-11-26 16:33 ` Steven Penny
@ 2015-11-26 17:20   ` Steven Penny
  2015-11-26 17:27     ` Helmut Karlowski
  0 siblings, 1 reply; 7+ messages in thread
From: Steven Penny @ 2015-11-26 17:20 UTC (permalink / raw)
  To: cygwin

On Thu, Nov 26, 2015 at 10:29 AM, Steven Penny wrote:
> echo 3.7 | awk '{printf "%.0f", $0}'

Another option

awk 'BEGIN {printf "%.0f", ARGV[1]}' 3.7

--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounding off real (floating point) values - bash to awk
  2015-11-26 17:20   ` Steven Penny
@ 2015-11-26 17:27     ` Helmut Karlowski
  0 siblings, 0 replies; 7+ messages in thread
From: Helmut Karlowski @ 2015-11-26 17:27 UTC (permalink / raw)
  To: cygwin; +Cc: cygwin

---------------------------------------------------
> On Thu, Nov 26, 2015 at 10:29 AM, Steven Penny wrote:
> > echo 3.7 | awk '{printf "%.0f", $0}'
> 
> Another option
> 
> awk 'BEGIN {printf "%.0f", ARGV[1]}' 3.7

#4: 

printf "%3.0f\n" 3.7

-Helmut


--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounding off real (floating point) values - bash to awk
  2015-11-26 13:30 Rounding off real (floating point) values - bash to awk Lester Anderson
  2015-11-26 15:32 ` Eliot Moss
  2015-11-26 16:33 ` Steven Penny
@ 2015-11-26 18:04 ` Achim Gratz
  2 siblings, 0 replies; 7+ messages in thread
From: Achim Gratz @ 2015-11-26 18:04 UTC (permalink / raw)
  To: cygwin

Lester Anderson writes:
[…]

By now, none of what you ask has anything to do with Cygwin anymore.
Please consider chosing a more appropriate forum for your questions.


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Waldorf MIDI Implementation & additional documentation:
http://Synth.Stromeko.net/Downloads.html#WaldorfDocs

--
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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: Rounding off real (floating point) values - bash to awk
  2015-11-26 15:32 ` Eliot Moss
@ 2015-12-03  9:14   ` Duncan Roe
  0 siblings, 0 replies; 7+ messages in thread
From: Duncan Roe @ 2015-12-03  9:14 UTC (permalink / raw)
  To: cygwin

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2015-12-03  9:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-26 13:30 Rounding off real (floating point) values - bash to awk Lester Anderson
2015-11-26 15:32 ` Eliot Moss
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

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).