admin管理员组

文章数量:1414949

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

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
Share Improve this question edited Apr 8, 2016 at 9:56 karaxuna asked Apr 8, 2016 at 9:49 karaxunakaraxuna 26.9k13 gold badges86 silver badges119 bronze badges 3
  • 1 Possible duplicate of JavaScript displaying a float to 2 decimal places – Rayon Commented Apr 8, 2016 at 9:54
  • @RayonDabre it's not duplicate, it's about excel formatting, not just formatting number – karaxuna Commented Apr 8, 2016 at 9:55
  • 1 If so, this is really not the way to ask the question.. – Rayon Commented Apr 8, 2016 at 9:56
Add a ment  | 

3 Answers 3

Reset to default 2

Just use .toFixed() method:

cell.z = Number(XLSX.SSF._table[2]).toFixed(3);//0.000

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

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

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

As I understand from the ments, 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.

本文标签: javascriptDecimal places when number format in excelStack Overflow