0

a = ["5.0", 2.25, 3.0, "4.0"] how to get a = [5.0, 2.25, 3.0, 4.0].

I can able to get a = [5, 2.25, 3, 4]. but i need like this [5.0, 2.25, 3.0, 4.0]

can any one help me. please,

Thanks

3
  • 3
    In JS (unlike other languages) there is no difference between 5 and 5.0 they're both just numbers Commented Jun 14, 2017 at 12:08
  • 1
    There's no such a number in JS. Commented Jun 14, 2017 at 12:08
  • Ah, now I see. See @Teemu and George's comment then Commented Jun 14, 2017 at 12:10

5 Answers 5

2

Well simply map the result of Number:

var a = ["5.0", "2.25", "3.0", "4.0"];

var b = a.map(Number);
console.log(b);

Note:

You can't preserve the 0 from 5.0 in a Number, it won't be a valid number.

You can use .toFixed(1)to preserve it, but the results will be strings:

var b = a.map(function(v){
    return Number(v).toFixed(1);
});

Demo:

var a = ["5.0", "2.25", "3.0", "4.0"];

var b = a.map(function(v){
    return Number(v).toFixed(1);
});
console.log(b);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks fot your effort @chsdk Actualy by using toFixed(1), and i pushed it in array. then I got ["5.0", 2.25, "3.0", 4.0] like this. but now i want to remove it and try to get [5.0, 2.25, 3.0, 4.0]
I know there is no diff 5, and 5.0. but my compare is not matching so i need 5.0. with out " ". :(
You can't just get 5.0, a number will be always stored without this 0 in JavaScript, so it won't be possible to achieve it without wrapping it as a string.
0

Map the values with a parsing call:

var a = ["5.0", 2.25, 3.0, "4.0"];
var b = a.map(function(c) {
  return parseFloat(c.toString());
});
console.log(b);

Comments

0

All numbers in JavaScript are floating point numbers, but when there is no decimal component, the runtime doesn't bother showing the decimal because, for example, 5.0 is exactly the same value as 5.

If you really want to see the numbers with this decimal, you can convert them back to strings (for display) and control the precision with the .toFixed() method, which operates on a number, but returns a string:

var a = ["5.0", 2.25, 3.0, "4.0"];

var b = a.map(Number);

b.forEach(function(num){
  console.log(num.toFixed(2));
});

Comments

0
var a = ["5.0", 2.25, 3.0, "4.0"];

a.map(function(b){
    return parseFloat(b).toFixed(1)
})

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
Note that this will drop decimals of the more accurate numbers to one as well ...
0

Basically Javascript does not return any value after decimal point if value is 0. You need to do it with programatically.

You can parse string with parseFloat and then you can use this function .toFixed() or .toString() so that it will give numbers with decimal point, even if 0 after decimal point.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.