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

Chapsterj
  • 6,483
  • 20
  • 70
  • 124
  • 1
    Replace them? `parseInt(value.replace(",",""))` – phpisuber01 Jun 17 '13 at 19:29
  • 2
    _without having to do a replace or something_ ? How is that even possible ? – Adil Shaikh Jun 17 '13 at 19:29
  • possible duplicate of [javascript parseFloat '500,000' returns 500 when I need 500000](http://stackoverflow.com/questions/3205730/javascript-parsefloat-500-000-returns-500-when-i-need-500000) – James Montagne Jun 17 '13 at 19:30
  • 1
    value.replace(",","") will only remove 1 comma and leave the other. – Chapsterj Jun 17 '13 at 19:30
  • Dupe: http://stackoverflow.com/questions/4083372/in-javascript-jquery-what-is-the-best-way-to-convert-a-number-with-a-comma-into – Paolo Bergantino Jun 17 '13 at 19:30
  • If you don't want to do a replace, your if statement won't work as expected. You need to do the replace to get a float / integer so your comparison will work as desired. – random_user_name Jun 17 '13 at 19:31
  • The simple fact is that, to a computer, "1,000" isn't a number; it's a string. (Exception: countries where the comma is a decimal separator, which obviously isn't what OP has in mind.) – Blazemonger Jun 17 '13 at 19:32

3 Answers3

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.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • 1
    @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. – George Cummins Jun 17 '13 at 19:31
  • 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... – Charlie Jun 17 '13 at 19:35
  • I compromised by adding a fiddle. – George Cummins Jun 17 '13 at 19:48
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/

DACrosby
  • 11,116
  • 3
  • 39
  • 51
-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/

Charlie
  • 11,380
  • 19
  • 83
  • 138
  • 1
    Won't work for `1,000,000`. Also, provide a radix to `parseInt`: `parseInt(n, 10)` – Blender Jun 17 '13 at 19:30
  • I'd still pass a radix to `parseInt`. If there are leading zeroes, some versions of Firefox will treat it as an octal. – Blender Jun 17 '13 at 19:32
  • What does the 10 mean "radix"? – Chapsterj Jun 17 '13 at 19:47
  • 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 http://stackoverflow.com/questions/9380039/default-parseint-radix-to-10) – Charlie Jun 17 '13 at 19:50
  • 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. – Charlie Jun 17 '13 at 19:52
  • If I use a value of 100,000.10 for example parseInt(n,10) removes the .10 at the end? – Chapsterj Jun 17 '13 at 20:00