[Tutor] round so only see 2 digits

dman dsh8290@rit.edu
Mon, 4 Mar 2002 15:35:12 -0500


On Mon, Mar 04, 2002 at 12:46:34PM -0600, Kojo Idrissa wrote:
[talk of version numbers]

| Here's the output:
| A:\Python>version.py
| My current age is 31.588722
| **********
| My Major Version Number is 3
| **********
| My Minor Version Number is 1
| **********
| 0.588722
| My Revision Number is 0.588722
| **********
| My Minor + Revision Number is 1.588722
| Kojo Idrissa: Version Number 3.1.588722
| 
| I'm trying to get rid of the leadin "0" on the Revision 
| Number.  My current "workaround" is to just add the Minor 
| and Revision numbers together.  Any ideas, other than 
| converting the value to a string and manipulating that?

If you can guarantee that all numbers will be less than one :

rev = ("%.6f" % .588722)[1:]
print rev

or, perhaps better,

rev = '.' + ("%.6f" % .588722).split( '.' )[1]
print rev


Alternatively, use integers for everything and format the result at
the end.  Recall that version numbers are not floating point numbers,
but rather dot-separated integers.


age = (time.time()-(3600*24*213))/(3600*24*365.25)
major = int( age/10 )
minor = int( age - (major*10) )
micro = int( ( (age-(major_ver*10)) - int(minor) ) * (10**6) )

print "Age is %d.%d.%d" % (major , minor , micro)


This is effectively what you had (without the locals just to make my
email shorter).  The change is for the "micro" number, instead
left-shift by 6 decimal places before rounding/truncation to int.

HTH,
-D

-- 

For society, it's probably a good thing that engineers value function
over appearance.  For example, you wouldn't want engineers to build
nuclear power plants that only _look_ like they would keep all the
radiation inside.
    (Scott Adams - The Dilbert principle)