PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, August 10, 2022

[FIXED] How to parse number with points and commas

 August 10, 2022     decimal, javascript, jquery, precision, web     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing