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


Groups > comp.lang.python > #31295

Re: simple string format question

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <adnothing@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'e.g.,': 0.07; 'subject:question': 0.08; 'rounds': 0.09; 'subject:string': 0.09; '2.00': 0.16; 'subject:simple': 0.16; '\xe9crit': 0.16; 'specify': 0.17; 'written': 0.20; 'cheers,': 0.23; 'tried': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:" 20': 0.26; 'no,': 0.29; 'maybe': 0.29; 'received:209.85.215.46': 0.30; 'point': 0.31; 'print': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'message-id:@gmail.com': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'received:194': 0.61; 'more': 0.63; "'2'": 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=7H10CQCLtRUJPZA1yF4+JTrDg1tFEUYU4pyPKomTu5Y=; b=sRjQDNazleJtE7lE0E4Ogg0Xz9p3R8MMLGSpO8wfmrD6oWU068RmvwuCitgqplIKPp qb4n9h3HAPubmgf7PmQA0vFltBQgMesbEVtC3tfI76dasByx1C+XJ5FVSZ0vjFsgRaOA tYwXOJouY7l3Zwn3InsJAclIzxuPGGY77xwgterypSOTDeS3dZnHFzpc7FCfg5TlhG9R qVn8XHqXIMB48yHggGWR8mtusJiOBBxm5SCXMRUtLL9InPvVdAs8BwYhgDW6cDVGO9dp +5OHg9bsC2nVAhkflEG7C1XJanSmImAFYEhpJaNkw5LEU8Px/o+t1ap1jBJ5NVmdNfiy u7vw==
Date Mon, 15 Oct 2012 14:58:09 +0200
From Adrien <adnothing@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20121010 Thunderbird/16.0.1
MIME-Version 1.0
To python-list@python.org
Subject Re: simple string format question
References <k5guje$ui$1@ger.gmane.org>
In-Reply-To <k5guje$ui$1@ger.gmane.org>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
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.2200.1350305892.27098.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1350305892 news.xs4all.nl 6845 [2001:888:2000:d::a6]:54577
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:31295

Show key headers only | View raw


Le 15/10/2012 14:12, Neal Becker a écrit :
> Is there a way to specify to format I want a floating point written with no more
> than e.g., 2 digits after the decimal?  I tried {:.2f}, but then I get all
> floats written with 2 digits, even if they are 0:
>
> 2.35 << yes, that's what I want
> 2.00 << no, I want just 2 or 2.

Maybe you're looking for "{:.3g}"

print "{:.3g}".format(2)
# '2'

print "{:.3g}".format(2.00)
# '2'

print "{:.3g}".format(2.35)
# '2.35'

print "{:.3g}".format(2.356)  # this rounds up
# '2.36'

Cheers,

-- Adrien

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


Thread

Re: simple string format question Adrien <adnothing@gmail.com> - 2012-10-15 14:58 +0200
  Re: simple string format question Piet van Oostrum <piet@vanoostrum.org> - 2012-10-24 23:48 -0400
    Re: simple string format question Neil Cerutti <neilc@norwich.edu> - 2012-10-25 12:49 +0000

csiph-web