469,903 Members | 1,736 Online

# Integer and Decimal Parts

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

--