admin管理员组

文章数量:1303061

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.

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.

Share Improve this question edited Nov 30, 2014 at 19:13 Travis asked Nov 30, 2014 at 3:47 TravisTravis 1,2841 gold badge16 silver badges35 bronze badges 13
  • 4 This may sound stupid, but why don't you get rid of the mas with a simple str.replace ? – Christian Bonato Commented Nov 30, 2014 at 3:50
  • Like @Bonatoc said - parseInt("5,100".replace(",","")); – Dimitar Dimitrov Commented Nov 30, 2014 at 3:53
  • Careful though, if your app needs i18n, cents ponctuations (decimal marks) differ from country to country : wikiwand./en/Decimal_mark#/Examples_of_use – Christian Bonato Commented Nov 30, 2014 at 3:56
  • 1 replace(/,/g, ''): Why does javascript replace only first instance when using replace? – Felix Kling Commented Nov 30, 2014 at 4: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 Commented Nov 30, 2014 at 4:06
 |  Show 8 more ments

3 Answers 3

Reset to default 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.

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(){....

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

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

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

The plete 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

本文标签: javascriptMake parseFloat convert variables with commas into numbersStack Overflow