You Are Here:

Community: Developer Discussion Boards

#1 Old How to print pretty (float) numbers? - 2009-03-11, 01:10

Join Date: Mar 2003
Posts: 936
Location: Espoo, Finland
JOM's Avatar
JOM
Offline
Forum Nokia Champion
Just run out of energy drink, so got to get some sleep. Stuck in problem, which should be easy, but haven't been able to solve. Can anyone help, basic non-s60 python stuff:

I have numbers, which I re-calculate occasionally. I want to present them in a readable, visually nice, way:

- All numbers are positive, more than or equal to zero
- All numbers are presented as text strings on-screen
- Full integers as integers only e.g. "100"
- Numbers less than zero with one leading zero e.g. "0.123"
- Max five decimals e.g. "3.33333"
- As few decimals as possible e.g. "3.45" instead of 3.45000
- Don't care whether 6.666 would be rounded as 6.66 or 6.67. Irrelevant.

How to do this? The number is always (for the moment) result of multiplication of two float numbers. The output format is controlled by sprintf format, which I thought I knew well enough, but obviously not this night... also reference book is archived in unknown location.

%0.5f would limit decimals to 5, but it's ALWAYS 5. Don't want any zeros at the end, how to get rid of those? Even this would be big help!

Btw this has to be as fast as possible, of course It's python and already running way too slow, about 5 secs an update. Furthermore 1.9.2 seems to be even slower

Recharging batteries,

--jouni
Reply With Quote

#2 Old Re: How to print pretty (float) numbers? - 2009-03-11, 06:39

Join Date: Sep 2005
Posts: 314
Location: Finland, Helsinki
aaaaapo
Offline
Regular Contributor
Quote:
Originally Posted by JOM View Post
%0.5f would limit decimals to 5, but it's ALWAYS 5. Don't want any zeros at the end, how to get rid of those?
Code:
>>> (u"%.5f" % 3.45000).rstrip("0")
u'3.45'
You probably still have to do some post-processing:
  • 0.00000 would be "0." so you may want to check zero separately
  • numbers bigger than 1000 are formatted with thousand separator, so in Finnish "locale" I get this result (without any way to change formatting):
    Code:
    >>> print "%.5f" % 12345.678
    12 345.67800
    In some other locale you may get even 12.345.67800.


--
Aapo Rista
http://code.google.com/p/pys60gps/
http://opennetmap.org/
Reply With Quote

#3 Old Re: How to print pretty (float) numbers? - 2009-03-12, 17:34

Join Date: Mar 2003
Posts: 936
Location: Espoo, Finland
JOM's Avatar
JOM
Offline
Forum Nokia Champion
Quote:
Originally Posted by aaaaapo View Post
Code:
>>> (u"%.5f" % 3.45000).rstrip("0")
u'3.45'
Thanx, this helped to solve the problem! I desided to keep the thousands separator on-screen as decorator and get the number for calculations elsewhere. Sample code below, just in case anyone needs it later:

Code:
        s = "%.5f" % float(g_currex[key][0])
        s = s.rstrip("0")
        if s[0] == '.':
            s = "0" + s
        if s[-1] == '.':
            s = s[:-1]
Quote:
Originally Posted by aaaaapo View Post
numbers bigger than 1000 are formatted with thousand separator, so in Finnish "locale" I get this result (without any way to change formatting):
Code:
>>> print "%.5f" % 12345.678
12 345.67800
In some other locale you may get even 12.345.67800.[/list]
Seen both, major problem.. This could mean that my app will not work in some locales at all Really would need some access or control to user's locate settings!

Thanx,

--jouni
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules

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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump
Similar Threads
Thread Thread Starter Forum Replies Last Post
[announce] sysagent 1.2.0 for 3rd cyke64 Python 7 2009-06-01 11:34
Guitar Tuner in Python cassioli Python 33 2009-04-01 15:37
How to send an image with attachment in email krtrivdi Python 1 2007-01-09 13:07
Sending an image over email jeclarim Python 3 2006-12-15 21:25
Editor to take Real or Float numbers Blkangel General Symbian C++ 2 2004-01-20 12:12

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d81449X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZpythonQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZuserE5ftagQSxpythonX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ