1

I recieve an string like;

1.234.567,89

I want 1234567.89

Comma is decimal delimiter.Point is thousands, and millions delimiter

I want to treat as a number.

I try replaces, but only works with first ".". And parseFloat. Also I try some regex that found here,but doesn't work for me

I want this;

    var numberAsString= '1.234.567,89';
    //Step to clean string and conver to a number to compare (numberAsString => numberCleaned)
    if (numberCleaned> 1000000) {alert("greater than 1 million");}

Any Idea? (Sorry if its a newbie question, but I dont found any solution in hours...)

2
  • 2
    Remove all the . characters, replace , with ., and then call parseFloat(). Commented May 5, 2020 at 15:01
  • While asking a question post all the code attempts that you have made. Please read through the help center, in particular How do I ask a good question? Commented May 5, 2020 at 15:05

2 Answers 2

5

You can use replace with g

const val = '1.234.567,89'.replace(/\./gi, '').replace(/,/, '.');
console.log(val)
console.log(typeof parseFloat(val))

Sign up to request clarification or add additional context in comments.

1 Comment

It works perfect!! I chose you as solution! thanks!
1

this should work for the current scenario. 1st remove the dots then replace the comma with dot.

let number = "1.234.567,89";

function parseNum(num){
    return num.replace(/\./g, '').replace(",", ".")
}


console.log(parseNum(number));

3 Comments

or Number.parseFloat('1.234.567,89'.split('.').join('').replace(',','.')) but yours is nicer :D
@Argee well there are obviously a lot of methods to achieve it but the better solution will be easy to understand and re implement.
Also try this soloution, and works fine, nice for getting more ideas!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.