Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42327 > unrolled thread

round off to two decimal & return float

Started byஆமாச்சு <amachu@amachu.me>
First post2013-03-30 13:22 +0530
Last post2013-03-30 02:30 -0700
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  round off to two decimal & return float ஆமாச்சு <amachu@amachu.me> - 2013-03-30 13:22 +0530
    Re: round off to two decimal & return float pyplexed <julius.welby@gmail.com> - 2013-03-30 02:30 -0700
    Re: round off to two decimal & return float pyplexed <julius.welby@gmail.com> - 2013-03-30 02:30 -0700

#42327 — round off to two decimal & return float

Fromஆமாச்சு <amachu@amachu.me>
Date2013-03-30 13:22 +0530
Subjectround off to two decimal & return float
Message-ID<mailman.3990.1364630137.2939.python-list@python.org>
Consider the scenario,

>> a = 10
>> "{0:.2f}".format(a)
'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.

--

Sri Ramadoss M

[toc] | [next] | [standalone]


#42329

Frompyplexed <julius.welby@gmail.com>
Date2013-03-30 02:30 -0700
Message-ID<e7b2b0a1-5055-4b20-8f11-2d960b785ace@googlegroups.com>
In reply to#42327
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

[toc] | [prev] | [next] | [standalone]


#42331

Frompyplexed <julius.welby@gmail.com>
Date2013-03-30 02:30 -0700
Message-ID<mailman.3993.1364636590.2939.python-list@python.org>
In reply to#42327
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web