我正在尝试确保这些组合中的任何一个 - 将始终显示没有小数或没有逗号的舍入数字。实施例
1,200.00 should output = 1200
12.33 should output = 12
123.34 should output = 123
1,434 should output = 1434
165,33 should output = 165
250.00 should output = 250
259,00 should output = 259
我试过了:
var currency = "$1,100.00";
var number = Number(currency.replace(/[^0-9\.]+/g,""));
我的最新消息:
var priceTotal = parseFloat(price.replace( /[^\d\.]*/g, ''));
但这对逗号不起作用。它必须始终显示,不带逗号,点或最后一位数字。
答案 0 :(得分:3)
请使用/[^0-9\.]/g
正则表达式替换逗号,使用indexOf
查找逗号的位置并使用parseInt
进行制作该字符串为整数。
请按照以下示例::
var a = '1,200.00';
var b = '12.33';
var c = '123.34';
var d = '1,434';
var e = '165,33';
var f = '250.00';
var g = '259,00';
$(document).ready(function () {
parseNumberCustom(a);
parseNumberCustom(b);
parseNumberCustom(c);
parseNumberCustom(d);
parseNumberCustom(e);
parseNumberCustom(f);
parseNumberCustom(g);
});
function parseNumberCustom(number_string) {
var new_number = parseInt(number_string.indexOf(',') >= 3 ? number_string.split(',')[0] : number_string.replace(/[^0-9\.]/g, ''));
console.log('Before==>' + number_string + ', After==>' + new_number);
}
答案 1 :(得分:2)
检查
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<script>
$(document).ready(function(){
$("#Convert").click(function(){
var currency1 = $("#mode").val()
if (currency1.indexOf(",") >= 0 && currency1.indexOf(".") >= 0)
{
var intvalue =(parseInt(currency1.replace(/[^0-9\.]/g, '')));
alert(intvalue)
}
if (currency1.indexOf(".") >= 0 && currency1.indexOf(",") < 0)
{
var intvalue = Math.floor( currency1 );
alert(intvalue)
}
if (currency1.indexOf(",") >= 0 && currency1.indexOf(".") < 0)
{
var streetaddress= currency1.substr(0, currency1.indexOf(','));
alert(streetaddress)
}
})
});
</script>
<body>
<div class="table-responsive container">
Enter Value:
<input type=Text id="mode" text="Switch Mode" class="Form-control" value="" >
<input type=Button id="Convert" text="Switch Mode" class="btn btn-primary" value="Convert" >
</div>
</body>
</html>
&#13;
答案 2 :(得分:1)
使用Math.round
方法对数字进行舍入或使用Math.floor
方法,以防您需要较低的值。
var number = Math.round(
currency
// convert the comma seperated decimal part to dot seperated
.replace(/\D(\d{2})$/, '.$1')
// replace all non-digit dot characters
.replace(/[^\d.]+/g, '')
);
['1,200.00', '12,33', '123.34', '1,434', '$1,100.16'].forEach(function(currency) {
var number = Math.round(currency.replace(/\D(\d{2})$/, '.$1').replace(/[^\d.]+/g, ""));
console.log(currency + ' => ' + number)
})
&#13;
仅供参考:无需在角色类中转义.
。
答案 3 :(得分:1)
尝试使用parseInt方法
var a = '1,200.00';
var b = '12.33';
var c = '123.34';
var d = '1,434';
var e = '165,33';
var f = '250.00';
var g = '259,00';
console.log(parseInt(a.replace(/\,(\d\d)$/, ".$1").replace(',','')));
console.log(parseInt(b.replace(/\,(\d\d)$/, ".$1").replace(',','')));
console.log(parseInt(c.replace(/\,(\d\d)$/, ".$1").replace(',','')));
console.log(parseInt(d.replace(/\,(\d\d)$/, ".$1").replace(',','')));
console.log(parseInt(e.replace(/\,(\d\d)$/, ".$1").replace(',','')));
console.log(parseInt(f.replace(/\,(\d\d)$/, ".$1").replace(',','')));
console.log(parseInt(g.replace(/\,(\d\d)$/, ".$1").replace(',','')));
&#13;
答案 4 :(得分:1)
如果输入总是有两位小数(当那里有任何小数时),这应该有效:
function toNumber(string) {
string = string.replace(/[^\d\,\.]/g, '');
let commaNotation = /^\d+(\.\d{3})?\,\d{2}$/.test(string);
let result = commaNotation ?
Math.round(parseFloat(string.replace(/[^\d\,]/g, '').replace(/,/, '.'))) :
Math.round(parseFloat(string.replace(/[^\d\.]/g, '')));
return result;
};
如果您不想在&gt; = 0.50时进行整理,则可以选择使用 Math.floor()
而不是 Math.round()