I am trying to make sure that any of these combinations - Will always display the rounded digit with no decimals or no commas. Example
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
I have tried:
var currency = "$1,100.00";
var number = Number(currency.replace(/[^0-9\.]+/g,""));
And my latest:
var priceTotal = parseFloat(price.replace( /[^\d\.]*/g, ''));
But this does not work well with comma. It must always be display without comma, dot or the last digits.
Please use /[^0-9\.]/g regex to replace comma, use indexOf for find the position of comma AND use parseInt to make that string into integer.
Please follow the below example::
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);
}
Check this
<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>
Use Math.round method to round a number or use Math.floor method in case you need the lower value.
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)
})
FYI : There is no need to escape . within the character class.