473,587 Members | 2,508 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 20354
Si <s.***@cwcom.ne t> 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(1234 56 / 1000);
var fracPart = 123456 % 1000;

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.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.ne t> 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(1234 56 / 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.ne t> 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(1234 56 / 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.ne t> 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%10 00

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.demo n.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demo n.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
1892
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 text file - I have had the same), it is loosing decimal parts. For example, let say, that from MySQL I have $line=123.45 Then if I will do this:
6
10534
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 differences between the databases servers ? Peter
3
19222
by: stevek | last post by:
How do I format an integer. Add commas. 1234565 1,234,565 TIA
4
9720
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 even after I have change the Fieldsize propert to Decimal with Scale = 2, the digits after the decimal are not seen. For eg. If the text was 16.27....
20
9133
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., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for...
11
9996
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 4(integer) 4.0(decimal) returns 4(integer)
3
12411
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 has more then two places before the decimal return zero. // Or if value1 has more then two places after the decimal return zero.
26
9451
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 to code to where the conversion itself happens. Code in scanfs just ignores return value from conversion procedures. More info in case of glibc...
6
12917
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 returns upper value after .51 decimal part. I mean; Dim number as integer = 12.51 returns 13.
0
7852
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7974
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8221
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6629
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5719
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3845
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1455
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1192
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.