0

For instance I have this number: 1.000,20 I want to convert it to 1000.20 or just remove everything behind the comma like this: 1000.

How do I achieve this? I did this:

console.log(num3.replace(/\[,.]/g, ''), 'test', num3.replace(/\,/g, ''));

Which results in: 0,00

Here is a Fiddle which console.logs the output on line 18 in the JS: https://jsfiddle.net/kn7ae9e2/1/

4
  • See Remove everything after a certain character Commented Jan 23, 2018 at 10:18
  • 1
    When you say "I have a number", I guess what you mean is "I have a string"?? Also, converting "1.000,20" --> "1000" is not only removing after the comma, you have also removed the ".". Is that also desired/intentional? Do you want the final result to be a string or a number? Please be very specific in what you are asking; this is too vague and open to interpretation. Commented Jan 23, 2018 at 10:19
  • num3.replace(/\./g, '').replace(/,/, '.') ? Commented Jan 23, 2018 at 10:20
  • Please explain the question with proper number formats. Commented Jan 23, 2018 at 10:21

2 Answers 2

1

You can replace the . first and then match everything before ,

"1.000,20".replace(/\./g, "").match(/\d+(?=,)/) //prints 1000

Demo

var output = "1.000,20".replace(/\./g, "").match(/\d+(?=,)/) //prints 1000
console.log( output );

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

2 Comments

This results: null it is indeed a string like tomLord mentions above than I tried this: Number(num3.replace(/\./g, "").match(/\d+(?=,)/g)) but this results in 0
@Sreinieren I have attached a demo.
0

You can split the string and recombine it like so:

num3="1.000,20";
num3.split(',')[0].split('.').join('') //"1000"

You can prepend it with the unary + operator to typecast it to a number

+num3.split(',')[0].split('.').join('') ///1000

If you want to preserve the value after the decimal point, you can do something like this:

function parseNumber(str){
    var splits = str.split(',');
    var preDec = splits[0].split('.').join('');
    return splits.length > 1? +(preDec+'.'+splits[1]): +preDec;
}

parseNumber("1.000,20"); //1000.2

Comments

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.