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 :: .yarnrc.yml get node module back 
Javascript :: how to edit a fil with vanilla js 
Javascript :: usestate hook with callback 
Javascript :: electron vue printer 
Javascript :: slice string javascript if has @ 
Javascript :: Webpack ReferenceError: process is not defined #871 electron vue 
Javascript :: remove last tag in dom javascript 
Javascript :: node js code for saving first middle and last name 
Javascript :: nest js crons 
Javascript :: javascript eingabe in inputfielder übernehmen 
Javascript :: like dislike node js 
Javascript :: react native fetch response code 
Javascript :: js string insert space 
Javascript :: javascript closure interview questions 
Javascript :: make shorter if statements with objects 
Javascript :: navigation scroll react 
Javascript :: on drop, drag, dragover event 
Javascript :: before in javascript 
Javascript :: javascript rest 
Javascript :: react-beforeunload react navive 
Javascript :: how to fetch data from another website in javascript 
Javascript :: javascript get last 2 item in array 
Javascript :: parseint() javascript 
Javascript :: js delete all from array 
Javascript :: delete array 
Javascript :: leaflet update marker icon 
Javascript :: reset event listener javascript 
Javascript :: javascript entries() method 
Javascript :: make forn awesome icon clickable in react 
Javascript :: local reference in angular 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =