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