473,796 Members | 2,872 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to compare two 20 digit numbers in javascript

hi frnds,
Im having two 20digit numbers, But while comparing those it
is giiving wrong ouput in javascript.

for example here is my code,
my secanrio is ,

~ If first 20 digit number is greater number than second 20 digit
number ,then it should return.

* And another important thing is, the two 20digit number will be same
but last one digit only change

Code:

<HTML>
<script language='javas cript'>
function numberCheck()
{
var val1=document.g etElementById(' txt1').value;
var val2=document.g etElementById(' txt2').value;
if(parseInt(val 1) parseInt(val2))
alert("Greater" )
else
alert("Correct" );

}
</script>
<body>
<input type=text id="txt1" value="18446744 073709551617" size=25>
<input type=text id="txt2" value="18446744 073709551616" size=25>
<input type=button onclick="number Check();" value="check">

</body>
</html>

So here txt1 value is greater than txt2 value, but it returning Correct
alert.
Please tel me reason why its returning like this,
Is there any restriction for comparing more than 20 digits

Jul 28 '06
11 17819
JRS: In article <11************ **********@b28g 2000cwb.googleg roups.com>
, dated Fri, 28 Jul 2006 02:07:30 remote, seen in
news:comp.lang. javascript, RobG <rg***@iinet.ne t.auposted :
>
ba************ *****@gmail.com wrote:
> Can u give me a suggestion.
That must seem very polite, to a Burmese.
>An algorithm is:

- get the values as strings (input elements will return strings)
- check they are both integers
But -79, +3, 123e45, 123e4567 are all integers. *Probably* he means
all-digit strings.
- remove leading zeros
Probably. But 077 = 0x3F = 63 = 0.63e2, maybe.
>function isBigger(a, b)
{
// Make sure they are integers
var re = new RegExp('\\D','g ');
I don't see that 'g' is needed, any non-digit fails an all-digit format.
if (re.test(a) || re.test(b)){
return undefined;
}

// Trim leading zeros
a = a.replace(/^0+/g,'');
b = b.replace(/^0+/g,'');
Ditto.
// Check lengths
if (a.length != b.length){
return (a.length b.length);
}

// Compare digits
var a = a.split('');
var b = b.split('');
for (var i=0, j=a.length; i<j; i++){
if (+a[i] b[i]) return true;
}
return false;
}
It could be better to compare a.charAt(i) with b.charAt(i); it saves
generating length+length objects. Perhaps I mean charCodeAt throughout.
I suspect you've not handled b<a, though he seems not to need that.
I think I'd go lower-level.

Take strings A & B.

From the Subject line, we can take it that the numbers consist only of
decimal digits, to be interpreted as decimal (if doubtful, use a
preliminary RegExp[*]).

We can also take it that they are of equal length; and, if not, it's
easy to prepend a string of zeroes to the shorter. At this stage they
can in fact be compared as strings.

Otherwise, loop through both strings, subtracting A.charAt[i] and
B.charAt[i]. At the first difference, return that difference. Finally
return 0 (the final difference; so it'd be nice to use
Dif = cA-cB ; if (!Dif) break ; ... return Dif ; ).
ISTM that charAt[i] should be swift given what ECMA seems to say about
all characters using 16 bits. If charAt[50] requires 100 or more bytes
to be considered, I'd not want to use it here.

ISTM that the strings could perhaps be broken up into ten-digit units
with a cunning RegExp (actually, cunning not needed where the length is
a multiple of 10) match, and the resulting strings compared numerically
in turn with unary +.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/>? JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 28 '06 #11
Hi john,

Thanks for suggestion, Its working fine.

Aug 3 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
32660
by: Rune Strand | last post by:
Hi, If I have a lot of integers and want do something with each digit as integer, what is the fastest way to get there? Eg. Make 12345 into an iterable object, like or "12345" (Btw: What is the English term for this process; itemize? tokenize? digitize? sequence?) Some examples:
4
5392
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm working on it but not making much progress. Is there any free code available anywhere? I know it...
11
2987
by: Nobody | last post by:
Heres the deal... I have an application where I have a list (as in a Windows list control, but thats not important) displayed to the user. I sort this list based on the list controls sort function (again, its not important that its Windows) which ends up calling a compare function in my code: int CompareFunc(char* str1, char* str2) { } this function returns -1, 0 or 1 which gets passed on to the internal quick
3
1667
by: Dan Gidman | last post by:
Okay all I have a problem. I have two list of adresses and phone numbers. I do not have control over the contents of the lists The only unique field between the two is the phone number. I need to be able to inner join the two lists on phone number. This would normally be straigt forward but the problem is that they are formated different and one of them does't even have a control on the formating. *Phone numbers are US phone...
3
21030
by: Matt | last post by:
if (123 > 33) will return true and if ("123" > 33) will return true So my question is, if the above behaviors are the same?? If string is a number, and compare with another number, it will be the same behavior as compare 2 numbers?
12
29474
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
14
8663
by: thehobbit | last post by:
Hi, Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and pass the result back to a calling program in COBOL? We were able to add up to 15 digit numbers without any problems, but we started facing issues once we go above 15 digits. Thanks, Venkat
10
1524
by: agendum97 | last post by:
I have three values: UInt64 a, b, c, d; I need to determine if: (a*b) <=(c*d). Naturally, just doing the multiplication and comparing would tell me the result. However, the values could potentially overflow. For example, if: a = UInt64.MaxValue; b = 2;
7
21601
by: harijay | last post by:
Hi I am a few months new into python. I have used regexps before in perl and java but am a little confused with this problem. I want to parse a number of strings and extract only those that contain a 4 digit number anywhere inside a string However the regexp p = re.compile(r'\d{4}')
0
9527
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10453
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9050
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7546
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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 we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.