By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,256 Members | 1,663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,256 IT Pros & Developers. It's quick & easy.

Integer and Decimal Parts

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.