最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Make parseFloat convert variables with commas into numbers - Stack Overflow

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>