round off to two decimal & return float

  • Thread starter ஆமாசà¯à®šà¯
  • Start date
À

ஆமாசà¯à®šà¯

Consider the scenario,
'10.00'

This returns a string 10.00. But what is the preferred method to retain
10.0 (float) as 10.00 (float)?

I am trying to assign the value to a cell of a spreadsheet, using
python-xlwt. I would like to have 10.00 as the value that is right
aligned. With text it is left aligned.
 
P

pyplexed

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
 
P

pyplexed

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top