0

Now decimal place is 2, I want to change it depending on variable, is it possible?

cell.v = parseFloat(cell.v),
cell.t = 'n';
cell.z = XLSX.SSF._table[2]; // 0.00 -> want to change to 0.000
karaxuna
  • 26,752
  • 13
  • 82
  • 117
  • 1
    Possible duplicate of [JavaScript displaying a float to 2 decimal places](http://stackoverflow.com/questions/3163070/javascript-displaying-a-float-to-2-decimal-places) – Rayon Apr 08 '16 at 09:54
  • @RayonDabre it's not duplicate, it's about excel formatting, not just formatting number – karaxuna Apr 08 '16 at 09:55
  • 1
    If so, this is really not the way to ask the question.. – Rayon Apr 08 '16 at 09:56

3 Answers3

2

Just use .toFixed() method:

cell.z = Number(XLSX.SSF._table[2]).toFixed(3);//0.000
Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
2

Just use a combination of parseFloat() and toFixed():

cell.z = parseFloat(XLSX.SSF._table[2]).toFixed(3);

ParseFloat() is designed specifically to turn strings into numbers.

Matthew
  • 462
  • 8
  • 20
1

As I understand from the comments, the OP wants to write a XLSX file using the js-xlsx library, with a number rendered with 3 decimals.

I achieve this as follows. I add the format code "0.000" to XLSX.SSF._table:

XLSX.SSF._table[164] = '0.000'

Then cell.z = '0.000' should work.

I don't know why the index 164. If there is another format, it goes to 165, then 166, etc.

Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225