Always Show 4 Decimal Places

Tag: xlwt , decimal Author: super2012 Date: 2013-04-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.

Other Answer1

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.