Connecting Tech Pros Worldwide Forums | Help | Site Map

Math.floor

Henke
Guest
 
Posts: n/a
#1: Jul 23 '05
I wanna round an value in a javascript Down to nearest hundred.
For example: 3234 = 3200 and 3890 = 3800
Always down and to hundred.
I have tried Math.floor but that just round down to ten.
Is there any way 2 do this??

Regards
/Henke



Mick White
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Math.floor


Henke wrote:
[color=blue]
> I wanna round an value in a javascript Down to nearest hundred.
> For example: 3234 = 3200 and 3890 = 3800
> Always down and to hundred.
> I have tried Math.floor but that just round down to ten.
> Is there any way 2 do this??
>[/color]

Math.floor(number/100)*100

Mick
McKirahan
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Math.floor


"Henke" <hlo@thomsonfakta.se> wrote in message
news:b3f1830e.0501130551.4a0c2966@posting.google.c om...[color=blue]
> I wanna round an value in a javascript Down to nearest hundred.
> For example: 3234 = 3200 and 3890 = 3800
> Always down and to hundred.
> I have tried Math.floor but that just round down to ten.
> Is there any way 2 do this??
>
> Regards
> /Henke[/color]

alert(parseInt(3234/100,10));
alert(parseInt(3890/100,10));


Lee
Guest
 
Posts: n/a
#4: Jul 23 '05

re: Math.floor


Henke said:[color=blue]
>
>I wanna round an value in a javascript Down to nearest hundred.
>For example: 3234 = 3200 and 3890 = 3800
>Always down and to hundred.
>I have tried Math.floor but that just round down to ten.
>Is there any way 2 do this??[/color]

Several, including n-n%100;

Dr John Stockton
Guest
 
Posts: n/a
#5: Jul 23 '05

re: Math.floor


JRS: In article <q_6dnbZ_hPYiHnvcRVn-jg@comcast.com>, dated Thu, 13 Jan
2005 08:22:05, seen in news:comp.lang.javascript, McKirahan
<News@McKirahan.com> posted :[color=blue]
>"Henke" <hlo@thomsonfakta.se> wrote in message
>news:b3f1830e.0501130551.4a0c2966@posting.google. com...[color=green]
>> I wanna round an value in a javascript Down to nearest hundred.
>> For example: 3234 = 3200 and 3890 = 3800[/color][/color]
[color=blue]
>alert(parseInt(3234/100,10));
>alert(parseInt(3890/100,10));[/color]

That involves an implicit conversion to String and an explicit
conversion back to Number; it should be slower than a pure mathematical
method. Also, it gives an incorrect answer.

If the OP happens to want a string result,
x = String(N/100).replace(/\..*./, '')+'00'
could be speed-tested; but the result may be unsatisfactory for N<100.

If negative numbers are possible, should -777 go to -700 or -800 ?

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread


Similar JavaScript / Ajax / DHTML bytes