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


Groups > comp.lang.python > #42333

Re: round off to two decimal & return float

References <515699DB.8060104@amachu.me> <CALk2KRU37Ri-EfwGKZK3MR-MHBoCuvx1u6DDj7BqBcav+9OqCA@mail.gmail.com>
From Roland Mueller <roland.em0001@googlemail.com>
Date 2013-03-30 11:43 +0200
Subject Re: round off to two decimal & return float
Newsgroups comp.lang.python
Message-ID <mailman.3995.1364636655.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

2013/3/30 Roland Mueller <roland.em0001@googlemail.com>

> Hello,
>
> 2013/3/30 ஆமாச்சு <amachu@amachu.me>
>
>> 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 assume you have a numeric value a and want to have a float with 2
> decimals. This can be achieved with the function round():
>
> Of course the representation of the float having a given number of
 decimals printed out is always a string. The float format does not imply
any formatting and Python's default formatting when a float is printed out
is to print as decimals as needed. Thus, using round() fives you a float
with maximal two decimals.

>>> a = 9.2022222
>>> a = round(a,2)
>>> a
9.2

>>> a = 9.2222222
>>> a = round(a,2)
>>> a
9.22


>>> a = 10
>
>
> >>> type(a)
> <type 'int'>
>
> >>> a = round(10,2)
>
>
>
> >>> type (a)
>
>
> <type 'float'>
>
>
> >>> a
>
>
> 10.0
>
> BR,
> Roland
>
>
>> 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
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: round off to two decimal & return float Roland Mueller <roland.em0001@googlemail.com> - 2013-03-30 11:43 +0200

csiph-web