469,903 Members | 1,736 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Integer and Decimal Parts

Si
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?

This is the sort of thing that is done using the "int" and "mod" function
in Excel

Cheers Si,
Jul 20 '05 #1
4 20160
Si <s.***@cwcom.net> writes:
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?

This is the sort of thing that is done using the "int" and "mod"
function in Excel


Just use Math.floor and % (modulus operator).

var intPart = Math.floor(123456 / 1000);
var fracPart = 123456 % 1000;

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Si
Great Thanks!

Si,

On 01 Nov 2003 19:54:32 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Si <s.***@cwcom.net> writes:
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?

This is the sort of thing that is done using the "int" and "mod"
function in Excel


Just use Math.floor and % (modulus operator).

var intPart = Math.floor(123456 / 1000);
var fracPart = 123456 % 1000;

/L


Jul 20 '05 #3
Si
Thats Great!

Thanks.

Si,

On 01 Nov 2003 19:54:32 +0100, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
Si <s.***@cwcom.net> writes:
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?

This is the sort of thing that is done using the "int" and "mod"
function in Excel


Just use Math.floor and % (modulus operator).

var intPart = Math.floor(123456 / 1000);
var fracPart = 123456 % 1000;

/L


Jul 20 '05 #4
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.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by gs | last post: by
3 posts views Thread by stevek | last post: by
11 posts views Thread by ChrisM | last post: by
3 posts views Thread by mohaaron | last post: by
26 posts views Thread by vid512 | last post: by
6 posts views Thread by kimiraikkonen | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.