Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #49283
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <joshua.landau.ws@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.089 |
| X-Spam-Evidence | '*H*': 0.82; '*S*': 0.00; 'subject:help': 0.08; 'string': 0.09; 'try:': 0.09; 'works.': 0.09; '{},': 0.09; 'cc:addr:python-list': 0.11; '"general': 0.16; '"it\'s': 0.16; 'formatted': 0.16; 'need:': 0.16; 'reliably': 0.16; 'wrote:': 0.18; 'skip:p 40': 0.19; 'example': 0.22; 'cc:addr:python.org': 0.22; 'skip:{ 20': 0.24; 'cc:2**0': 0.24; 'skip:" 20': 0.27; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'forgot': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'fine,': 0.31; 'prints': 0.31; 'really,': 0.31; 'know.': 0.32; 'run': 0.32; 'quite': 0.32; 'url:python': 0.33; 'running': 0.33; 'guess': 0.33; 'skip:s 30': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; '8bit%:9': 0.36; 'skip:" 50': 0.36; 'thanks': 0.36; 'url:org': 0.36; 'wrong': 0.37; 'so,': 0.37; 'level': 0.37; 'ahead': 0.38; 'url:library': 0.38; 'work?': 0.38; 'little': 0.38; 'does': 0.39; 'help,': 0.39; 'skip:. 10': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'full': 0.61; 'url:3': 0.61; 'back': 0.62; 'kind': 0.63; 'skip:n 10': 0.64; 'subject:Need': 0.64; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'of:': 0.68; 'hey,': 0.75; '"it': 0.84; '*and*': 0.84; 'ugly,': 0.84; 'numbers:': 0.91; 'choice.': 0.93; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type:content-transfer-encoding; bh=+AKOkZBhQ4s9XVmFH3d32TnCJ5YgitvLmY+ElGAu4Vc=; b=HFWj6zJ/+e8d2mird/PGJuQwYcdqQ+dtA2TFDoSSIOSo+E7rQNnp+gyG7H8A1vy2kL 3GmAx9tTF95s5Gz8argxEaFnLxdh+b7PJUr5l3OOepCjzXFgPiJcjSd8LyLoDGfNK93Z 6wJSk5t9+haelX8skfQ++/kGdbzjb5BvaciS2M8FOFbioHfEFBULqxYzk3t93ewqf6+N Im7zpPQKL+MmgLVcauUfsQOGJ1/sI1ZT68loCi7+uXGWap5uhoZmnIx2F5O+GK8HtN6x r8VcT8D4xmnAc5cUv4iQpQKEGA+8db+FdvORchDrYY10q6hY9XfCe039o4ia010kCxuW BGAg== |
| X-Received | by 10.152.4.232 with SMTP id n8mr2907390lan.29.1372287979295; Wed, 26 Jun 2013 16:06:19 -0700 (PDT) |
| MIME-Version | 1.0 |
| In-Reply-To | <6cd21bc6-8437-4aec-99b0-59a1d9c7bd9b@googlegroups.com> |
| References | <55bb3a29-7c8a-402c-8fb5-d12b5ee6330b@googlegroups.com> <6cd21bc6-8437-4aec-99b0-59a1d9c7bd9b@googlegroups.com> |
| From | Joshua Landau <joshua.landau.ws@gmail.com> |
| Date | Thu, 27 Jun 2013 00:05:39 +0100 |
| Subject | Re: Need help removing trailing zeros |
| To | PyNoob <bandcamp57@gmail.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list <python-list@python.org> |
| 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.3906.1372287981.3114.python-list@python.org> (permalink) |
| Lines | 81 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1372287981 news.xs4all.nl 15880 [2001:888:2000:d::a6]:50381 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:49283 |
Show key headers only | View raw
On 26 June 2013 23:21, PyNoob <bandcamp57@gmail.com> wrote:
> Sorry about that... And thanks for your help, but I don't quite understand.
That's fine, but...
> Would that make it off your example print("{:g}".format(1.0))?
I don't understand this sentence.
But, hey, I forgot to check what level you were working at -- there's
little point running ahead of what you know.
Did you try:
print("{:g}".format(1.0))
? It works for me. So, yes, that's what you want.
So instead of:
print("The quotient of", A, "and", B, "is: ", Answer)
you want
print("The quotient of", A, "and", B, "is: ", "{:g}".format(Answer))
See how I just used "{:g}".format as some kind of magic-fixing-power?
Well, why does it work?
[See http://docs.python.org/3/library/stdtypes.html#str.format and the
sub-links for a more full explanation]
Run each of these in an interpreter:
"{} {} {} {} {}".format("This", "is", "a", "formatted", "string!")
"It is really useful: I have {} cows and {}
sheep".format(number_of_cows, number_of_sheep)
"It gives me back a formatted {}, which I can
print".format(type("".format()).__name__)
"I can also give things indexes: {3} {2} {1} {0}".format("Print",
"in", "reverse", "order")
"I can also give things names: {egg} {ham}
{flies}".format(egg="Are", ham="you", flies="there?")
"It's not just {:!<10}".format("that")
"It lets me choose how I want things to be printed: {0:@^6},
{0:~>10}, {0:£<8}".format("Hi")
"And for numbers: {0:e}, {0:.10%}, {0:#.1f}".format(123.456)
So you just want the best formatter; see
[http://docs.python.org/3/library/string.html#format-specification-mini-language]
Your best choice is "{:g}" which just means "general format".
Note that you can also write your prints as so:
print("The quotient of {} and {} is: {:g}".format(A, B, Answer))
Whether you prefer it is your choice.
------
In regards to .rstrip: It will only work on strings; so you need to
convert like so:
str(1.0).rstrip("0").rstrip(".")
And note that:
1) My .rstrip("0.") was wrong and foolish (try str(10).rstrip("0."))
2) This needs the string to be reliably formatted in this style:
"{:#f}" *and* requires it to be a float.
So really, you'd need:
"{:#f}".format(float(number)).rstrip("0").rstrip(".")
Which is ugly, but I guess it works.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Need help removing trailing zeros bandcamp57@gmail.com - 2013-06-26 15:02 -0700
Re: Need help removing trailing zeros Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-26 23:14 +0100
Re: Need help removing trailing zeros PyNoob <bandcamp57@gmail.com> - 2013-06-26 15:21 -0700
Re: Need help removing trailing zeros Joshua Landau <joshua.landau.ws@gmail.com> - 2013-06-27 00:05 +0100
Re: Need help removing trailing zeros PyNoob <bandcamp57@gmail.com> - 2013-06-26 19:30 -0700
csiph-web