Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: simple string format question

Thread Tools

Re: simple string format question

Dave Angel
Posts: n/a
On 10/15/2012 08:29 AM, Chris Rebert wrote:
> On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker <(E-Mail Removed)> 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):
> Cheers,
> Chris



Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: raw format string in string format method? Chris Angelico Python 3 03-01-2013 12:00 AM
Re: raw format string in string format method? Rick Johnson Python 0 02-28-2013 11:06 PM
Re: raw format string in string format method? Peter Otten Python 0 02-28-2013 03:41 PM
simple string format question Neal Becker Python 0 10-15-2012 12:12 PM
Simple error : method format(String, Object[]) is not applicable for the arguments (String, String) ankur Java 1 08-27-2007 06:31 AM