-1

I am needing format a data with 8 decimal numbers, eg:

843332 -> 0.00843332
123456789 -> 1.23456789
9876543210 -> 98.76543210

How could I perform this task in JavaScript?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Felipe Simões
  • 31
  • 1
  • 1
  • 4
  • Possible duplicate of [Format number to always show 2 decimal places](https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places) – Sebastian Simon Jul 19 '18 at 03:17

2 Answers2

1

Just divide by 1e8:

const translate = num => num / 1e8;
console.log(
  [
    843332,
    123456789,
    9876543210
  ]
    .map(translate)
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1

function myFunction() {
 var value = ["843332", "123456789", "9876543210"];
    
    for(i =0; i < value.length; i++){
     document.getElementById("demo").innerHTML += (value[i] /100000000).toFixed(8) + " <br />";
    }
    
}
<p>Click button to generate</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

Please try this.

function myFunction() {
 var value = ["843332", "123456789", "9876543210"];

 for(i =0; i < value.length; i++){
    document.getElementById("demo").innerHTML += (value[i]/100000000).toFixed(8) + " <br />";
 }
}
lss
  • 23
  • 8