一律显示4个小数位 [英] Always Show 4 Decimal Places

查看:179
本文介绍了一律显示4个小数位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用xlwt强制某个单元始终显示4个小数位?不管小数点后的实际数字是多少。我已经进行了一些搜索,可以找到一个始终减少到2个小数位的字符串,但是我需要在工作表上显示某些单元格才能显示4。

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.

推荐答案

使用xlwt样式。这是一个简单的示例:

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')

或者您可以使用 xlwt.easyxf 定义样式。希望有帮助。

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

这篇关于一律显示4个小数位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆