1

I have a input filed that allows a user to place a number value which is then transformed into a string value to allow for commas to be added to values above a thousand. for example,

value = 1,000

The problem rises when a number is above 1000000 I want to throw an error. Right now I have an if statement.

if( value > 100000){
//throw error
}

the problem is its not working because the value is a string when the commas are added. say my value is 1000,001.00 I tried using parseInt on the value but it turns it to 1000 Is there a way I can use my if without having to do a replace or something like that on the commas before I do my if statement.

Thanks

7

3 Answers 3

2

Integer values cannot contain thousands separators (such as commas). If you wish to perform mathematical operations on a value

  • remove the commas from the string
  • convert the string to an integer
  • perform the operation
  • re-insert the commas as needed

If you are willing to reconsider the use of replace(), see a demonstration of how this can be accomplished.

Sign up to request clarification or add additional context in comments.

3 Comments

@cale_b I would, but the most obvious method would be to use replace() which the OP specifically rejected. Since the OP is aware of that method, I will allow him/her to determine the best way to accomplish the task.
I feel that the reason why the OP doesn't want to replace is because it looks complicated, but you have to remove them somehow, and replacing them with nothing does in turn remove them. So...
I compromised by adding a fiddle.
0

Here's a handy function to convert your strings into integers for comparison:

function getInt(num){
    var numAsString = num + "",
        numAsInt = parseInt(numAsString.replace(/,/g,""), 10);
    return numAsInt;
}

Note that I force the input to a string first, then parse it as a number. This way the same function works even if you do send a number to it for some reason. Same idea for using float:

function getFloat(num){
    var numAsString = num + "",
        numAsInt = parseFloat(numAsString.replace(/,/g,""));
    return numAsInt;
}

Note the lacking radix on parseFloat (", 10").

Usage:

if (getFloat("1,345,920.5") > 1000000)
    $("#out").append ("String converted to number for comparison!");

http://jsfiddle.net/daCrosby/w6uUY/2/

Comments

-1

Even though you don't want to, you're probably going to have to replace the comma:

num = "1,000"
parseFloat(num.replace(/,/g,""));

http://jsfiddle.net/rpPrh/3/

6 Comments

Won't work for 1,000,000. Also, provide a radix to parseInt: parseInt(n, 10)
I'd still pass a radix to parseInt. If there are leading zeroes, some versions of Firefox will treat it as an octal.
What does the 10 mean "radix"?
If you use parseInt with a string that begins with a 0, then some browsers might interpret it as an octal. So 014 would "parseInt()" as 12 (paraphrased from here stackoverflow.com/questions/9380039/…)
Also, why is this downvoted? This is no different from George Cummins's answer, except I started off with an example. "Removing the commas from the string" is accomplished through a replace.
|

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.