JRS: In article <op**************@news.eclipse.co.uk>, seen in

news:comp.lang.javascript, Si <s.***@cwcom.net> posted at Sat, 1 Nov

2003 18:46:35 :-

Hi.

After the prompt reply to my message yeasterday (Problem with Opera an IE),

I have another newbie question.

How do I separate the integer and decimal part of a number, ie. If I had

123456, and divided it by 1000 = 123.456,

how do I obtain 123 and 456 as seperate variables?

Question : do you want to start with the 123456, or the 123.456? Should

the results be numbers or strings? If the initial number had been

123450, would you want 45 or 450 for the second result? If it had been

123007, would you want 7 or 007?

It is always best to define the problem completely in words, illustrated

by salient examples.

Note that 123.456 cannot be exactly represented as a Number; it is not

easy to be sure that multiplying such by 1000 will always give a whole

number. If it ever gives less than the whole number, then Math.floor()

should give the next lower number.

Number 123 comes from Math.floor(123.456)

Number 456 comes from Math.round(123.456%1*1000)

123.456*1000%1000

If the original number 123456 is known to be integer, it would be better

to start with that and do only exact operations. If it is actually

already available as string, use string operations.

OK = /^(\d+)(\d\d\d)$/.test(123456) // true

AnS = [RegExp.$1, RegExp.$2] // '123','456'

AnN = [+RegExp.$1, +RegExp.$2] // 123,456

One can. of course, define Int and Mod functions, for convenience of

expression.

--

© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©

<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript

<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.

<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.