Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Re: simple string format question

 
 
Adrien
Guest
Posts: n/a
 
      10-15-2012
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

 
Reply With Quote
 
 
 
 
Piet van Oostrum
Guest
Posts: n/a
 
      10-25-2012
Adrien <(E-Mail Removed)> 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 <(E-Mail Removed)>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      10-25-2012
On 2012-10-25, Piet van Oostrum <(E-Mail Removed)> wrote:
> Adrien <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

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



Advertisments