Mr.D
Mr.D

Reputation: 7873

Xlsxwriter, how to increase decimal precision of float number?

I have created this kind of float format for cells:

floating_point_bordered = workbook.add_format({'num_format': '#,##0', 'border': 1})

worksheet.write(f'G{row}', position.price, floating_point_bordered)

When I generate xlsx file the I got this following result:

Image one

However the real value of this float is:

Image two

How to force float cells to show their values if increased precision?

Upvotes: 1

Views: 8849

Answers (1)

jmcnamara
jmcnamara

Reputation: 41554

XlsxWriter uses Excel's numeric formats so you should figure out the format you need in Excel first.

It is probably something like #,##0.00. For example:

import xlsxwriter

workbook = xlsxwriter.Workbook("test.xlsx")
worksheet = workbook.add_worksheet()

price = 879.65421

floating_point_bordered = workbook.add_format({'num_format': '#,##0.00', 'border': 1})

worksheet.write(2, 1, price, floating_point_bordered)

workbook.close()

Output: enter image description here

However, the numeric format needs to be in the US locale with "grouping/thousands" separator is "," and the "decimal" point is "." (dot). See this section of the XlsxWriter docs on Number Formats in different locales for more information.

Upvotes: 4

Related Questions