Always Show 4 Decimal Places

无人久伴 提交于 2020-01-24 12:42:07

问题


Is it possible to force a cell to always show 4 decimal places using xlwt? Regardless of the number actually present after the decimal place. I have done some searching and I can find a string to always reduce to 2 decimal places, but I need certain cells on my sheet to show 4.


回答1:


Use xlwt styles. Here's a simple example:

import xlwt


book = xlwt.Workbook()
sheet = book.add_sheet("test")

decimal_style = xlwt.XFStyle()
decimal_style.num_format_str = '0.0000'

sheet.write(0, 0, 1.23456789, decimal_style)
sheet.write(0, 1, 1.01, decimal_style)

book.save('test.xls')

Or you can use xlwt.easyxf for defining styles. Hope that helps.



来源:https://stackoverflow.com/questions/16176211/always-show-4-decimal-places

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!