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

485
Views
How to replace dots to commas and commas to dots in js

I have a simple question, I would like to replace dots to commas and commas to dots in a string, example:

var example1 = "10.499,99" // What I have
var result1 = "10,499.49" // What I need

var example2 = "99.999.999.999,99" // What I have
var result2 = "99,999,999,999.99" // What I need

I already tried to use this replace:

value.replace(/,/g, '.')

But it only changes all commas to dots.

Any idea ?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You can't do it at the same time. If you use replace function to change it from dot(.) -> comma(,) you will get your new string where a comma is replaced by a dot. But when you again try to do vice versa every comma will be replaced with a dot.

So what you can do is Replace the comma with a different string/symbol something like replace(/,/g , "__") then replace the dots with the same expression you are using above replace(/\./g, ',') and that replace those new string/(__) in our example to dot(.) replace(/__/g, '.')

var example2 = "99.999.999.999,99" example2.replace(/,/g , "__").replace(/\./g, ',').replace(/__/g, '.') //output : '99,999,999,999.99'

Happy Coding

about 4 years ago · Juan Pablo Isaza Report

0

Use the variant of the replaceAll function that takes a function as the second parameter. Use it to replace all occurrences of either dot or comma. The function will choose to replace dots with commas and commas with dots.

function switchDotsAndCommas(s) {

  function switcher(match) {
    // Switch dot with comma and vice versa
    return (match == ',') ? '.' : ',';
  }

  // Replace all occurrences of either dot or comma.
  // Use function switcher to decide what to replace them with.
  return s.replaceAll(/\.|\,/g, switcher);
}


console.log(switchDotsAndCommas('10.499,99'));
console.log(switchDotsAndCommas('99,999,999,999.99'));

about 4 years ago · Juan Pablo Isaza Report

0

Use

value.replace(".", ",");
about 4 years ago · Juan Pablo Isaza 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!