Converting Array of Strings to floats (?!?!) [duplicate] - javascript

This question already has answers here:
Parsing numbers with a comma decimal separator in JavaScript
(3 answers)
Closed 3 years ago.
I have an array like this:
["3.789,33", "2.702,17", "481,21", "1.149,44", "3.037,85", "44,24", "524,49", "1.378,42", "32,02"]
and I would like to convert it to an array of numbers/floats.
I tried .map(Number) and also parseInt() and parseFloat() but didn't seem to work out.
Can anybody help?

You need to replace . by empty string and , by . before parsing it to float
let arr = ["3.789,33", "2.702,17", "481,21", "1.149,44", "3.037,85", "44,24", "524,49", "1.378,42", "32,02"]
let final = arr.map(v=> parseFloat(v.replace(/\./g,'').replace(',','.')))
console.log(final)

Related

Replace dot in a number [duplicate]

This question already has answers here:
Removing everything except numbers in a string
(7 answers)
Closed 3 years ago.
I want to replace the dot in a number in Javascript with regular expression; if country_temp is, for example, 1.234, how can I put the value 1234 in country_temp2?
I have tried the following:
const country_temp2 = country_temp.replace(/[a-zA-Z]*\**\s*\./g, "")
but it's not working.
Edit: the original string is composed by characters, asterisk, a number and a dot
Sorry, but I have written in a very fast way.
Try this:
country_temp.replace(/\./g,'')

Explode string without separators [duplicate]

This question already has answers here:
How to get character array from a string?
(14 answers)
Closed 7 years ago.
I have a string like 12345 and I need to explode it to [1,2,3,4,5]. Normally, I would use split, but there are no separators in here. I could use substr (getting length, making loops), but I will probably have a lot of those strings, so it may not be good for performance. What can I do?
Simply use an empty string as a delimiter:
"12345".split('');

JavaScript / jQuery: how to split string with multiple values [duplicate]

This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 8 years ago.
I have a string that always contains 8 variable values that are separated with a hypen (-) like in the following example:
5-2-2-2-2-2-2-1
What is the best way to split this into 8 separate values so that I can use each of them in a variable if the values can be either an integer or the value 'n/a' ?
Many thanks for any help with this, Tim.
var str = '5-2-2-2-2-2-2-1';
var parts = str.split('-');
for (var i=0;i<parts.length;i++){
console.log(parts[i]);
}
You are searching for the String.split() method

Formatting Hex in Javascript [duplicate]

This question already has answers here:
How can I pad a value with leading zeros?
(76 answers)
How to output numbers with leading zeros in JavaScript? [duplicate]
(6 answers)
Closed 8 years ago.
How can I format a hex to be displayed always with 4 digits in javascript?
For example, I converting a decimal to hex :
port = 23
function d2h(d) {return (+d).toString(16);}
d2h(port)
I am successfully able to convert "23" to the hex value "17". However, I would like to be formatted like this "0017" (with four digits -- appending 0's before the 17).
All information would be greatly appreciated.
Here is an easy way:
return ("0000" + (+d).toString(16)).substr(-4);
Or:
return ("0000" + (+d).toString(16)).slice(-4);
You can use sprintf
http://www.diveintojavascript.com/projects/javascript-sprintf
I havent tried it yet but something like this should work:
sprintf("%.4X", d)

How to check two values of text box using JavaScript function [duplicate]

This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 9 years ago.
I have used document.getElementByID("textboxname").value to compare but it checks character by character as it considers integer value a string value.
I like to know how to convert document.getElementByID("textboxname").value string notation to integer notation so that it will be easy to compare integer values.
Thanks in advance
use parseInt("your string") to convert it into valid integer
Use parseInt(document.getElementByID("textboxname").value, 10);

Categories

Resources