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
3 Answers
Reset to default 2Just 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
版权声明:本文标题:javascript - Decimal places when number format in excel - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745157972a2645289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论