1

I'm trying to get parseFloat to convert a userInput (prompt) into a number.

For example:

var userInput = prompt("A number","5,000") 
function parse_float(number) {
    return parseFloat(number)
}

When userInput = 5,000, parse_Float(userInput) returns 5.

However, if the user was inputting a value to change something else (ie: make a bank deposit or withdrawl) Then I to work properly, parse.Float(userInput) needs to return 5000, not 5. If anyone could tell me how to do this it would help me so much. Thanks in advance.

Travis
  • 1,274
  • 1
  • 16
  • 33
  • 4
    This may sound stupid, but why don't you get rid of the commas with a simple str.replace ? – Christian Bonato Nov 30 '14 at 03:50
  • Like @Bonatoc said - `parseInt("5,100".replace(",",""));` – Dimitar Dimitrov Nov 30 '14 at 03:53
  • Careful though, if your app needs i18n, cents ponctuations (decimal marks) differ from country to country : http://www.wikiwand.com/en/Decimal_mark#/Examples_of_use – Christian Bonato Nov 30 '14 at 03:56
  • I totally wrote that wrong, but thanks anyway – Travis Nov 30 '14 at 04:03
  • 1
    `replace(/,/g, '')`: [Why does javascript replace only first instance when using replace?](http://stackoverflow.com/q/1967119/218196) – Felix Kling Nov 30 '14 at 04:06
  • 1
    @wyattbergeron1 Yeah that's javascript it replaces the first occurrence only, here is how to do it for all: `"567,763,321".replace(/,/g,"");` – Dimitar Dimitrov Nov 30 '14 at 04:06
  • What is the syntax `parse.Float` supposed to do? –  Nov 30 '14 at 04:11
  • @Bonatoc It's currently a 'game' that i'm working on. It says in the beginning that it will use U.S. standard measurements punctuation etc. – Travis Nov 30 '14 at 04:12
  • @wyattbergeron1 Does your program also accept commas in the wrong place? For example, do `5,0,0,0` and `,,,5000,,,` parse as `5000` as well? – Lambda Fairy Nov 30 '14 at 04:20
  • @lambdaFairy no, it doesn't Ill figure that out, thanks for pointing it out. – Travis Nov 30 '14 at 04:22
  • @DimitarDimitrov That code won't change it if the commas are in the wrong spot. for example, 5,6,7,7,6,3,321 any idea how to fix that? – Travis Nov 30 '14 at 04:25
  • It occurred to me that you've only used integers so far, but call `parseFloat`. Do you want to accept decimal points as well? If not, `parseInt` is better. – Lambda Fairy Nov 30 '14 at 04:47
  • @LambdaFairy it's set to acccept decimals too, but it automatically accepts them. Only reason I don't use parseInt – Travis Nov 30 '14 at 14:26

3 Answers3

5

Your answer is close, but not quite right.

replace doesn't change the original string; it creates a new one. So you need to create a variable to hold the new string, and call parseFloat on that.

Here's the fixed code:

function parseFloatIgnoreCommas(number) {
    var numberNoCommas = number.replace(/,/g, '');
    return parseFloat(numberNoCommas);
}

I also renamed the function to parseFloatIgnoreCommas, which better describes what it does.

Lambda Fairy
  • 13,814
  • 7
  • 42
  • 68
  • Doesn't work, it gives me what js default does, the first instance. If you find a way to fix this, edit this and/or tell me. Thanks! – Travis Nov 30 '14 at 04:49
  • Whoops, I accidentally tried to parse the original string rather than the new one. The code should work now. – Lambda Fairy Nov 30 '14 at 04:53
2

This is the function I use to scrub my user inputted numbers from a form. It handles anything a user may put in with a number like $ or just accidentally hitting a key.

I copied the following out of an object:

cleanInput : function(userValue){
    //clean the user input and scrub out non numerals
    var cleanValue = parseFloat(userValue.replace(/[^0-9\.]+/g,""));    
    return cleanValue;
},

To make it non-object just change the first line to cleanInput(){....

techiegirl
  • 21
  • 1
0

I have put together info from the comments to form a basic answer:

The answer seems to simply be to set parse_float to run :

number.replace(/,/g, "")
return parseFloat(number)

The complete code would look like this:

var userInput = prompt("A number","523,000,321,312,321") 
function parse_float(number) {
    number.replace(/,/g, "")
return parseFloat(number)
}

returns: 523000321312321

Travis
  • 1,274
  • 1
  • 16
  • 33