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


Groups > comp.lang.python > #31296 > unrolled thread

Re: simple string format question

Started byDave Angel <d@davea.name>
First post2012-10-15 09:02 -0400
Last post2012-10-15 09:02 -0400
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: simple string format question Dave Angel <d@davea.name> - 2012-10-15 09:02 -0400

#31296 — Re: simple string format question

FromDave Angel <d@davea.name>
Date2012-10-15 09:02 -0400
SubjectRe: simple string format question
Message-ID<mailman.2201.1350306208.27098.python-list@python.org>
On 10/15/2012 08:29 AM, Chris Rebert wrote:
> On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker <ndbecker2@gmail.com> wrote:
>> 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.
> Not that I can find. Seems you'll have to implement it yourself.

To do it yourself, probably best to use a temp string value, created by
format "{: f}".  (Notice the space before the f, to reserve space for
the sign)  Then, slice that value to length 4 .  Finally, in your actual
format, use :5s for a format.  This should add the blanks for padding,
so other columns still line up.

>
> In the event that your project uses Django, there happens to be a
> template tag for this (pass it -2 in your case):
> https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs#floatformat
>
> Cheers,
> Chris


-- 

DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web