Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

655
Views
Remove decimal and comma and digits from price in jQuery

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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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);
}
about 4 years ago · Santiago Trujillo Report

0

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>

about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!