473,511 Members | 16,730 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 20348
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1888
by: gs | last post by:
Sorry if it will be second post, but after few hours I still can't see my previous post. I have problem with decimal parts on Solaris (PHP+ZendOptimizer). When I get value from MySQL (or from...
6
10523
by: Peter Blatt | last post by:
Does 5 represent the total numer of digits (including the fractional portion) or only the number of places BEFORE the decimal point? Moreover does the number include the decimal point? Are there...
3
19218
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
4
9711
by: italia | last post by:
I changed the Fieldsize Property from text to Long Integer and Decimal Places = 6. I had decimals in the original field. But after the transfer, the digits after the decimals are gone. Now...
20
9110
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
11
9984
by: ChrisM | last post by:
Hi Guys, I'm looking for a function that will return the lowest integer that is greater than or equal to a decimal number. eg. 3.1(decimal) returns 4(integer) 3.9(decimal) returns...
3
12403
by: mohaaron | last post by:
Hello All, Does anyone know of a way to test a decimal for the number of places it has before and after the decimal? I need to do something like this. decimal value1 = 172.45; // If value1...
26
9400
by: vid512 | last post by:
hi. i wanted to know why doesn't the scanf functions check for overflow when reading number. For example scanf("%d" on 32bit machine considers "1" and "4294967297" to be the same. I tracked...
6
12899
by: kimiraikkonen | last post by:
Hi, Sorry if this is so basic but i wanted to know the correct way of displaying a value's "only" integer part. I mean; if i declare a value as integer which is actually a decimal number, it...
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.