Velocity Reviews > Rounding a Number to a String with N decimal places

Rounding a Number to a String with N decimal places

Dr John Stockton
Guest
Posts: n/a

 08-15-2004

When rounding (or truncating) a number which may be either positive or
negative, it is sometimes necessary to round either towards plus
infinity or towards minus infinity. But those are special cases.

Normally, however, rounding should be symmetrical about zero; rounding X
should give the same digits as rounding -X, for all X.

Some of the signed routines which have been quoted here and elsewhere
are unsymmetrical.

--
<URL:http://jibbering.com/faq/> JL / RC : FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.

G Roydor
Guest
Posts: n/a

 08-15-2004

Dr John Stockton a écrit:
> When rounding (or truncating) a number which may be either positive or
> negative, it is sometimes necessary to round either towards plus
> infinity or towards minus infinity. But those are special cases.
>
> Normally, however, rounding should be symmetrical about zero; rounding X
> should give the same digits as rounding -X, for all X.

Non

2 < 2.x < 3 => 2 ou 3

-3 < -2.x < -2 => 3 ou 2

GR

>
> Some of the signed routines which have been quoted here and elsewhere
> are unsymmetrical.
>