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.
- 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
3 Answers
Reset to default 5Your 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
版权声明:本文标题:javascript - Make parseFloat convert variables with commas into numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741701095a2393292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论