Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42332
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'value,': 0.04; 'assign': 0.07; 'float': 0.07; 'see.': 0.07; 'subject:two': 0.07; 'string': 0.09; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '1):': 0.16; '2),': 0.16; 'aligned.': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'url:py': 0.16; 'url:secure': 0.16; 'url:simplistix': 0.16; 'url:svn': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'value.': 0.19; '>>>': 0.22; 'import': 0.22; 'preferred': 0.22; 'header:User- Agent:1': 0.23; 'cell:': 0.24; 'headers': 0.24; 'header': 0.24; 'compare': 0.26; 'pass': 0.26; 'excel': 0.26; 'header:X -Complaints-To:1': 0.27; 'cells': 0.31; 'text': 0.33; 'style': 0.33; 'actual': 0.34; 'convert': 0.35; 'but': 0.35; 'method': 0.36; 'example,': 0.37; 'to:addr:python-list': 0.38; 'track': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:x 10': 0.40; 'numbers': 0.61; 'url:co': 0.67; 'subject: & ': 0.68; '8bit%:100': 0.72; 'adapted': 0.84; 'skip:\xe0 20': 0.84; 'subject:round': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: round off to two decimal & return float |
| Date | Sat, 30 Mar 2013 10:44:04 +0100 |
| Organization | None |
| References | <515699DB.8060104@amachu.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="UTF-8" |
| Content-Transfer-Encoding | 8Bit |
| X-Gmane-NNTP-Posting-Host | p508492f5.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3994.1364636626.2939.python-list@python.org> (permalink) |
| Lines | 53 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1364636626 news.xs4all.nl 6852 [2001:888:2000:d::a6]:56436 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:42332 |
Show key headers only | View raw
ஆமாச்சு wrote:
> 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)?
You can use round() to convert 1.226 to 1.23
>>> round(1.225, 2)
1.23
for example, but 10.0 and 10.00 are the same float value -- there's no way
to keep track of the number of digits you want to see.
> 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.
You can pass 10.0 as the cell value and apply a format to the cell:
# adapted from
#https://secure.simplistix.co.uk/svn/xlwt/trunk/xlwt/examples/num_formats.py
import xlwt
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet('Example')
numbers = [10, 1.0/6]
headers = ["raw", "rounded", "formatted", "rounded and formatted"]
style = xlwt.XFStyle()
style.num_format_str = "0.00"
for column, header in enumerate(headers):
worksheet.write(0, column, header)
for row, value in enumerate(numbers, 1):
worksheet.write(row, 0, value)
worksheet.write(row, 1, round(value, 2))
worksheet.write(row, 2, value, style)
worksheet.write(row, 3, round(value, 2), style)
workbook.save('tmp.xls')
Move over the cells with the cursor in Excel to compare what is displayed
with the actual value.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: round off to two decimal & return float Peter Otten <__peter__@web.de> - 2013-03-30 10:44 +0100
csiph-web