Wednesday, August 10, 2022

[FIXED] How to parse number with points and commas

Issue

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...)


Solution

You can use replace with g

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



Answered By - brk
Answer Checked By - Mildred Charles (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.