0

I want to convert my Array from this :

[ '0.500000', '0.333333', '0.166667' ]

to this :

[ 0.500000, 0.333333, 0.166667 ]

I tried +, Number, parseInt, parseFloat but none of them work.

Ervin Szilagyi
  • 14,274
  • 2
  • 25
  • 40
Arezou Saremian
  • 508
  • 3
  • 8
  • 1
    You can't keep the trailing zeroes once it is converted to a number. – kelsny Apr 24 '22 at 12:29
  • 2
    Numbers don't have trailing zeros. They don't have a format. – jabaa Apr 24 '22 at 12:29
  • sounds to me like you're confused as to what you actually want. If you want formatted numbers to display in a UI, then what you already have is ideal - you want to keep them as strings. If you're interested in the underlying *number*, then 0.5 and 0.50000 are identical, which is why JS won't show you any difference. – Robin Zigmond Apr 24 '22 at 12:43
  • There is no such thing as `0.500000` in Javascript. The internal representation will always be `0.5`. – connexo Apr 24 '22 at 12:48
  • If you want to make it clear to, say, a maintainer of your code that you are working to 6 decimal places you could use 500000e-6 etc, but it makes no difference to the system. Can you say a bit more about why you need the representation you have given? – A Haworth Apr 24 '22 at 16:46

1 Answers1

0

As noted in the comments - you cannot retain the zero at the end unless it remains a string - if you convert the array values to numbers - you will lose all trailing zero's. If you need t o perform calculations - then you need to parse the strings into numbers when performing the calculation

but there is a workaround - if all the numbers have the same decimal places - you can use Math.pow() to multiply them by that power to get actual numbers.

This will convert your string array into a number array but with the numbers being mutiplied by 1 * (10 * the number of decimal places).

You can then use the values for performing calculations or whatever you need - just remember to divide by that 100000 to use them. Note that the toFixed() will convert the number to a string and provide the trailing zeros based on the number of decimal places you require.

That said - this is a hacky workaround - I would simply convert them to numbers, perform my calculation and then pad the trailing zeros with toFixed(decimalPlaces). Simple is always best.

const arr = [ '0.500000', '0.333333', '0.166667' ];

const decimalPlaces = 6;

const numArr = arr.map(x => x * (1 * Math.pow(10, decimalPlaces)));



console.log(numArr);
  // gives [500000, 333333, 166667]

const stringArr = numArr.map(y => (y / (1 * Math.pow(10, decimalPlaces))).toFixed(decimalPlaces)) 

console.log(stringArr);
  // gives ["0.500000","0.333333","0.166667"]
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • I try to answer an algorithmic question and my answer is not accepted because my elements are strings. This again gives me an array of string elements. What is the answer? – Arezou Saremian Apr 24 '22 at 12:46
  • @ArezouSaremian What you're asking for is impossible. Either you misunderstood the question or it doesn't support JavaScript. – jabaa Apr 24 '22 at 13:00
  • _I try to answer an algorithmic question and my answer is not accepted_ --> seems like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – jsN00b Apr 24 '22 at 13:24