round off to two decimal & return float

pyplexed julius.welby at gmail.com
Sat Mar 30 05:30:08 EDT 2013


Hi Sri.

I'm not familiar with the xlwt module, but I think you are confusing two different things here.

Generally spreadsheets separate out how they handle the value in a cell (the value) and how that value is displayed (the format). This means that the you leave the cell value unchanged when you tell Excel separately that the number should be displayed as, for example, a percentage, a date, in scientific notation, or as a float with a specified number of decimal places etc.

Try converting the integer to a floating point number using float(a), and see what the default format for floats looks like when you open the output in Excel. It may be OK for you. If not, take a look at the python-xlwt/examples folder. num_formats.py shows how to apply Excel number formats to cells.

By the way, if you are doing this kind of thing frequently or at any kind of scale, you may find it easier to use something like the pandas module to do your data manipulation and then you can use pandas' DataFrame.to_excel method which allows you to define a format for floats in the output, and also provides many other options.

HTH



More information about the Python-list mailing list