help-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: round to 0 or 5 at the 3 decimal place


From: Neil R. Ormos
Subject: Re: round to 0 or 5 at the 3 decimal place
Date: Fri, 4 Mar 2022 12:20:22 -0600 (CST)

Peng Yu wrote:

> printf("%.3d", ...) rounds a number to the 3 decimal place. When the
> number is less than 1, I want it to be rounded to the 3 decimal place
> but the allowed number at that decimal place is only 0 or 5. I could
> write some code based on conditional test to do this job. But
> considering this might not be a very rare use case, is there an easy
> way to do it?

gawk 'function r53(a){return int(a*200+0.5)/200}; \
  BEGIN{ for (i=1; i<=1000; i++) { \
  r=rand(); printf "%12f  %12.3f\n", r, r53(r) }}' 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]