convert string with dot or comma as decimal separator to number in javascript
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
}
}
Also in JavaScript:
- Title
- how to wait till jquery post request has been made
- Category
- JavaScript
- Title
- angularjs socket.io
- Category
- JavaScript
- Title
- Javascript change element class
- Category
- JavaScript
- Title
- angular flex layout
- Category
- JavaScript
- Title
- create an element jquery
- Category
- JavaScript
- Title
- document ready
- Category
- JavaScript
- Title
- how to parse json
- Category
- JavaScript
- Title
- async await javascript push
- Category
- JavaScript
- Title
- create element javascript with class
- Category
- JavaScript
- Title
- get current screen name react navigation
- Category
- JavaScript
- Title
- convert jquery to string
- Category
- JavaScript
- Title
- index and id togtgher angularjs
- Category
- JavaScript
- Title
- in javascript check is is an array or not
- Category
- JavaScript
- Title
- how to make an express server
- Category
- JavaScript
- Title
- insert into specific array index
- Category
- JavaScript
- Title
- coin change problem all combinations javascript
- Category
- JavaScript
- Title
- copy to clipboard javascript
- Category
- JavaScript
- Title
- inheritance in javascript
- Category
- JavaScript
- Title
- discord.js dm user
- Category
- JavaScript
- Title
- get attribute value jquery href
- Category
- JavaScript
- Title
- array index javascript show only first 2 elements
- Category
- JavaScript
- Title
- generate new component angular
- Category
- JavaScript
- Title
- compare NaN in javascript if condititon
- Category
- JavaScript
- Title
- edje js not pre compile for node 12 version
- Category
- JavaScript
- Title
- how to create response time router node js
- Category
- JavaScript
- Title
- check if localstorage key exists js
- Category
- JavaScript
- Title
- How to make a function run only after 2 events has triggered js
- Category
- JavaScript
- Title
- Angularjs datetime
- Category
- JavaScript
- Title
- change input placeholder text jquery
- Category
- JavaScript
- Title
- changing columns for table requires doctrine dbal install doctrine/dbal
- Category
- JavaScript
- Title
- async await react stackoverflow
- Category
- JavaScript
- Title
- 'Link' is not defined react/jsx-no-undef
- Category
- JavaScript
- Title
- angular list contains property
- Category
- JavaScript
- Title
- execute js with innerhtml
- Category
- JavaScript
- Title
- contact form7 404 wp-json feedback
- Category
- JavaScript
- Title
- Javascript clear canvas
- Category
- JavaScript
- Title
- how to draw a triangle using javascript
- Category
- JavaScript
- Title
- default pool size in sequelize
- Category
- JavaScript
- Title
- async await javascript
- Category
- JavaScript
- Title
- how to display image in html from json object
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- add elements to an array with splice
- Category
- JavaScript
- Title
- facebook integration in node.js
- Category
- JavaScript
- Title
- first element of array js
- Category
- JavaScript
- Title
- how to create seeds in sequelise
- Category
- JavaScript
- Title
- do more than one thing at start of or loop javascript
- Category
- JavaScript
- Title
- array data structure javascipt interview questions
- Category
- JavaScript
- Title
- how to iterate object inside object in javascript
- Category
- JavaScript
- Title
- how to check if variable is array in javascript
- Category
- JavaScript
- Title
- datatables server side
- Category
- JavaScript
- Title
- document queryselector click event
- Category
- JavaScript
- Title
- how to hide javascript element by class
- Category
- JavaScript
- Title
- get the size of the screen javascript
- Category
- JavaScript
- Title
- create react component class
- Category
- JavaScript
- Title
- edit onclick event
- Category
- JavaScript
- Title
- iterate through nested object javascript
- Category
- JavaScript
- Title
- await async sleep
- Category
- JavaScript
- Title
- inline z-index react
- Category
- JavaScript
- Title
- codemirror get object from textarea
- Category
- JavaScript
- Title
- append element javascript
- Category
- JavaScript
- Title
- dom get all tags
- Category
- JavaScript
- Title
- how to use for of in javascript
- Category
- JavaScript
- Title
- how to get the div value in jquery
- Category
- JavaScript
- Title
- hooks in react
- Category
- JavaScript
- Title
- expo app.json
- Category
- JavaScript
- Title
- clear input field value javascript
- Category
- JavaScript
- Title
- angular email validation
- Category
- JavaScript
- Title
- event import in angular
- Category
- JavaScript
- Title
- how to push object in array using for loop javascript
- Category
- JavaScript
- Title
- how to add row in angular dynamically
- Category
- JavaScript
- Title
- how to use the javascript console
- Category
- JavaScript
- Title
- javascript array to comma separated list
- Category
- JavaScript
- Title
- how to create a server in node js
- Category
- JavaScript
- Title
- how to make a function in javascript
- Category
- JavaScript
- Title
- discord js get badge user
- Category
- JavaScript
- Title
- how to add an array to another array in javascript
- Category
- JavaScript
- Title
- how to write and read a text file in jdiscord.js
- Category
- JavaScript
- Title
- disable right click javascript
- Category
- JavaScript
- Title
- how to use if else inside jsx in react
- Category
- JavaScript
- Title
- firestore set a document
- Category
- JavaScript
- Title
- how to check how many strings are in a sentence javascript
- Category
- JavaScript
- Title
- get element size javascript
- Category
- JavaScript
- Title
- array of in javascript
- Category
- JavaScript
- Title
- difference between foreach and map in js
- Category
- JavaScript
- Title
- array find method javascript
- Category
- JavaScript
- Title
- for Loop continue
- Category
- JavaScript
- Title
- array remove first element javascript
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- angular 8 to 9
- Category
- JavaScript
- Title
- console.log in jsx
- Category
- JavaScript
- Title
- How do you start a Node Application, if the entry file is index.js:
- Category
- JavaScript
- Title
- how to remove an class in javascript
- Category
- JavaScript
- Title
- casl react
- Category
- JavaScript
- Title
- input not detecting text
- Category
- JavaScript
- Title
- get url react
- Category
- JavaScript
- Title
- geojson longitude latitude order
- Category
- JavaScript
- Title
- how to print console in javascript
- Category
- JavaScript
- Title
- flutter vs react native
- Category
- JavaScript
- Title
- express js continous GET /json/version
- Category
- JavaScript
- Title
- get args nodejs
- Category
- JavaScript