简体   繁体   中英

Format excel numbers at two decimals with python

I would like to format numbers in excel export at 2 decimal places but only for view, and keep original values in behind as in the picture, number to be shown with two decimal places but when you click on the cell to be shown whole value. I have used XlsxWriter and add_format,and it is formating at 2 decimals but do not keep original values. Can someone suggest how it can be done and if it is posssible?

Picture example in excel:

excel中的图片示例

I have used XlsxWriter and add_format,and it is formating at 2 decimals but do not keep original values.

That can't be the case. XlsxWriter doesn't truncate the numbers that it handles in any way, apart from formatting them with %.16G in the same way that Excel does.

For example:

import xlsxwriter

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

my_format = workbook.add_format({'num_format': '0.00'})

worksheet.write(0, 0, 29.159573953, my_format)

workbook.close()

Output :

在此处输入图像描述

As you can see the full number is displayed in the formula bar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM