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


Groups > comp.lang.python > #89542

Re: str more precise in 3 as 2.7

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!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.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'from:addr:yahoo.co.uk': 0.04; 'output': 0.05; 'sys': 0.07; 'default.': 0.09; 'expression:': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:2.7': 0.09; 'python': 0.11; 'language.': 0.14; 'did:': 0.16; 'prepared.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'language': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'import': 0.22; 'header:User-Agent:1': 0.23; 'precise': 0.24; 'earlier': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'gives': 0.31; "skip:' 10": 0.31; 'once,': 0.31; 'url:python': 0.33; 'moment': 0.34; 'but': 0.35; 'url:org': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'bad': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'received:org': 0.40; 'worry': 0.60; 'numbers': 0.61; 'url:3': 0.61; 'simply': 0.61; 'our': 0.64; 'subject:more': 0.64; 'more': 0.64; 'different': 0.65; 'charset:windows-1252': 0.65; 'between': 0.67; 'received:2': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: str more precise in 3 as 2.7
Date Wed, 29 Apr 2015 12:50:59 +0100
References <87twvzch2u.fsf@Equus.decebal.nl>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-2-98-195-171.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0
In-Reply-To <87twvzch2u.fsf@Equus.decebal.nl>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.82.1430308275.3680.python-list@python.org> (permalink)
Lines 40
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430308275 news.xs4all.nl 2842 [2001:888:2000:d::a6]:35132
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89542

Show key headers only | View raw


On 29/04/2015 12:24, Cecil Westerhof wrote:
> I am trying to make my modules also work under Python 3. I found that
> str is more precise in Python 3. The expression:
>      str(134 / 6.0)
> gives in 2.7.8:
>      '22.3333333333'
> and in 3.4.1:
>      '22.333333333333332'
>
> Was not very hard to solve:
>      if python_version == 3:
>          output06[5] = '22.333333333333332'
>          output10[5] = '23.333333333333332'
>          output10[8] = '24.77777777777778'
>
> Earlier I did:
>      import sys
>
>      # For when the difference between 2 and 3 is important
>      python_version = sys.version_info[0]
>
> At the moment I use it only once, but it is never bad to be prepared.
>

They simply output different numbers of digits by default.  Just control 
the number of digits that you want to see and in this case you needn't 
worry again about Python 2 vs 3 differences.  See either

https://docs.python.org/3/library/string.html#formatspec

or

https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

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


Thread

str more precise in 3 as 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 13:24 +0200
  Re: str more precise in 3 as 2.7 Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-04-29 12:50 +0100
  Re: str more precise in 3 as 2.7 Chris Angelico <rosuav@gmail.com> - 2015-04-29 21:55 +1000
    Re: str more precise in 3 as 2.7 Cecil Westerhof <Cecil@decebal.nl> - 2015-04-29 14:09 +0200
  Re: str more precise in 3 as 2.7 Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-04-30 14:29 +1000

csiph-web