Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

convert string number with commas decimal to number javascript

parseFloat(str.replace(',','.').replace(' ',''))
Comment

convert string with dot or comma as decimal separator to number in javascript

function realParseFloat(s)
{
    s = s.replace(/[^d,.-]/g, ''); // strip everything except numbers, dots, commas and negative sign
    if (navigator.language.substring(0, 2) !== "de" && /^-?(?:d+|d{1,3}(?:,d{3})+)(?:.d+)?$/.test(s)) // if not in German locale and matches #,###.######
    {
        s = s.replace(/,/g, ''); // strip out commas
        return parseFloat(s); // convert to number
    }
    else if (/^-?(?:d+|d{1,3}(?:.d{3})+)(?:,d+)?$/.test(s)) // either in German locale or not match #,###.###### and now matches #.###,########
    {
        s = s.replace(/./g, ''); // strip out dots
        s = s.replace(/,/g, '.'); // replace comma with dot
        return parseFloat(s);
    }
    else // try #,###.###### anyway
    {
        s = s.replace(/,/g, ''); // strip out commas
        return parseFloat(s); // convert to number
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: js sort by date 
Javascript :: how to delete element in list javascript 
Javascript :: simple javascript code 
Javascript :: dynamic import javascript 
Javascript :: js number format 
Javascript :: javascript shuffle string 
Javascript :: google places autocomplete just cities 
Javascript :: loop over string js 
Javascript :: user focus on tab javascript 
Javascript :: add sass to react 
Javascript :: jquery datatable get column values in array 
Javascript :: sort javascript number array with duplicates 
Javascript :: nodejs javascript heap out of memory 
Javascript :: get cookie in javascript 
Javascript :: sequelize findone 
Javascript :: javascript get gps location 
Javascript :: JSON Web Token (JWT) set expire time in node js 
Javascript :: jquery onload function for div 
Javascript :: check if string contains lowercase javascript 
Javascript :: js convert array of arrays to array 
Javascript :: return symmetric difference of the array javascript 
Javascript :: short if 
Javascript :: angular findindex object based array 
Javascript :: google sheets api javascript 
Javascript :: get params from route vuejs 
Javascript :: jquery set hidden field value 
Javascript :: getting average of array javascript 
Javascript :: jquery when iframe is loaded 
Javascript :: filter out object in array using two arguments 
Javascript :: open pdf in browser javascript 
ADD CONTENT
Topic
Content
Source link
Name
9+2 =