7

I need to compare two Integers which could exceed Integer range limit. How do I get this in javascript. Initially, I get the value as String, do a parseInt and compare them.

var test = document.getElementById("test").value;
var actual = document.getElementById("actual").value;
if ( parseInt(test) == parseInt(actual)){
  return false;  
}

Any options to use long ? Also, which is best to use parseInt or valueOf ??

Any suggestions appreciated,

Thanks

1
  • How big is that number going to be? Commented Jul 3, 2012 at 9:09

3 Answers 3

5

You'd better to assign the radix. Ex. parseInt('08') will give 0 not 8.

if (parseInt(test, 10) === parseInt(actual, 10)) {
Sign up to request clarification or add additional context in comments.

3 Comments

How does this help with them being larger numbers than Javascript can handle?
Thanks. Will this suffice even if the value exceeds 32 bit or 64 bit Integer limit..
@User: "Numbers in JavaScript are "double-precision 64-bit format IEEE 754 values", according to the spec." from MDN. So the maximum integer it can store is 53-bit. If the actual number is larger than 53 bit, you should not convert it to Javascript number.
4

Leave them in String and compare (after you have cleaned up the string of leading and trailing spaces, and other characters that you consider safe to remove without changing the meaning of the number).

The numbers in Javascript can go up to 53-bit precision. Check whether your number is within range.

Since the input is expected to be integer, you can be strict and only allow the input to only match the regex:

/\s*0*([1-9]\d*|0)\s*/

(Arbitrary leading spaces, arbitrary number of leading 0's, sequence of meaningful digits or single 0, arbitrary trailing spaces)

The number can be extract from the first capturing group.

Comments

1

Assuming integers and that you've already validated for non-numeric characters that you don't want to be part of the comparison, you can clean up some leading/trailing stuff and then just compare lengths and if lengths are equal, then do a plain ascii comparison and this will work for any arbitrary length of number:

function mTrim(val) {
    var temp = val.replace(/^[\s0]+/, "").replace(/\s+$/, "");
    if (!temp) {
        temp = "0";
    }
    return(temp);
}

var test = mTrim(document.getElementById("test").value);
var actual = mTrim(document.getElementById("actual").value);

if (test.length > actual.length) {
    // test is greater than actual
} else if (test.length < actual.length) {
    // test is less than actual
} else {
    // do a plain ascii comparison of test and actual
    if (test == actual) {
        // values are the same
    } else if (test > ascii) {
        // test is greater than actual
    } else {
        // test is less than actual
    }
}

2 Comments

How about empty string (or string of only spaces)? It will compare equal to "0".
@nhahtdh - Yes, I've assumed that two empty strings (either empty or only blanks) are to be considered zero and equal (the OP doesn't specify what they want to happen in that case). The OP needs a step before this to decide what is and isn't valid for this type of comparison (as the first sentence in my answer explains). I didn't attempt to include that in my answer because it wasn't part of the question and the OP didn't specify what validation rules should be used.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.