2015-05-22 01:03:57 -0400, Chris F.A. Johnson:
> On 2015-05-22, Wayne wrote:
> > On 5/21/2015 2:37 AM, Hongyi Zhao wrote:
> >> On Thu, 21 May 2015 01:44:21 -0400, Chris F.A. Johnson wrote:
> >>
> >>> Assuming that the number is in the decimal form, N.N:
> >>>
> >>> var=12.34 int=${var%.*}
> >
> > To round a number in Bash:
> >
> > printf '%.0f\n' 3.6 # prints "4"
>
> Or, to store the result in a variable:
>
> printf -v varname '%.0f\n' 3.6
Note that in that case you can drop the \n:
printf -v varname %.0f 3.5
> > The "${var%.*}" expansion truncates, but
> > does not round.
>
> I should have noticed that the OP asked for it to be rounded not
> truncated.
[...]
Note that the number if first converted to the internal floating
point format and then subject to the rounding operator, which
can give some simingly inconsistent results:
$ printf '%.0f\n' 15e-1 25e-1 3.5 4.5
2
2
4
4
The rint() of ksh93 or zsh have a similar limitation, but not
the round() of ksh93 it would seem.
--
Stephane