Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
simple string format question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Neal Becker  
View profile  
 More options Oct 15 2012, 8:12 am
Newsgroups: comp.lang.python
From: Neal Becker <ndbeck...@gmail.com>
Date: Mon, 15 Oct 2012 08:12:31 -0400
Local: Mon, Oct 15 2012 8:12 am
Subject: simple string format question
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Chris Rebert  
View profile  
 More options Oct 15 2012, 8:30 am
Newsgroups: comp.lang.python
From: Chris Rebert <c...@rebertia.com>
Date: Mon, 15 Oct 2012 05:29:59 -0700
Local: Mon, Oct 15 2012 8:29 am
Subject: Re: simple string format question

On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker <ndbeck...@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.

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=ol...

Cheers,
Chris


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Adrien  
View profile  
 More options Oct 15 2012, 8:58 am
Newsgroups: comp.lang.python
From: Adrien <adnoth...@gmail.com>
Date: Mon, 15 Oct 2012 14:58:09 +0200
Local: Mon, Oct 15 2012 8:58 am
Subject: Re: simple string format question
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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Oct 15 2012, 9:03 am
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Mon, 15 Oct 2012 09:02:58 -0400
Local: Mon, Oct 15 2012 9:02 am
Subject: Re: simple string format question
On 10/15/2012 08:29 AM, Chris Rebert wrote:

> On Mon, Oct 15, 2012 at 5:12 AM, Neal Becker <ndbeck...@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=ol...

> Cheers,
> Chris

--

DaveA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Piet van Oostrum  
View profile  
 More options Oct 24 2012, 11:48 pm
Newsgroups: comp.lang.python
From: Piet van Oostrum <p...@vanoostrum.org>
Date: Wed, 24 Oct 2012 23:48:02 -0400
Local: Wed, Oct 24 2012 11:48 pm
Subject: Re: simple string format question

Adrien <adnoth...@gmail.com> writes:
> print "{:.3g}".format(2.356)  # this rounds up

But:

>>> print "{:.3g}".format(12.356)
12.4
>>> print "{:.3g}".format(123.356)

123

--
Piet van Oostrum <p...@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Neil Cerutti  
View profile  
 More options Oct 25 2012, 8:49 am
Newsgroups: comp.lang.python
From: Neil Cerutti <ne...@norwich.edu>
Date: 25 Oct 2012 12:49:20 GMT
Local: Thurs, Oct 25 2012 8:49 am
Subject: Re: simple string format question
On 2012-10-25, Piet van Oostrum <p...@vanoostrum.org> wrote:

> Adrien <adnoth...@gmail.com> writes:

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

> But:

>>>> print "{:.3g}".format(12.356)
> 12.4
>>>> print "{:.3g}".format(123.356)
> 123

  The precision is a decimal number indicating how many digits
  should be displayed after the decimal point for a floating
  point value formatted with 'f' and 'F', or before and after the
  decimal point for a floating point value formatted with 'g' or
  'G'. For non-number types the field indicates the maximum field
  size - in other words, how many characters will be used from
  the field content. The precision is not allowed for integer
  values.

So g will print a specific number of significant digits, so it
won't do what Adrien wants.

And f will print a fixed number of digits after the decimal
point, so it won't do want Adrien wants.

Adrien, you will need to do some post-processing on fixed point
output to remove trailing zeroes.

>>> print("{:.2f}".format(2.1).rstrip('0'))
2.1
>>> print("{:.2f}".format(2.127).rstrip('0'))

2.13

--
Neil Cerutti


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »