[Tutor] Help on float & round.

Decibels decibelshelp@charter.net
Wed Jun 11 00:15:03 2003


Hello,

        New to the list and having a problem with float & round or my lack of understanding.

Basic: I am getting stock prices, it puts it in a list, then I convert it to strings, floating and integers.
The problem I am having is that it converts the string to a float fine. But rounding it off and saving
it as a variable isn't working like I expected.

Examples:
        
        # Original number before conversion was 70.70 as string.
        # I get the 3rd item of the list called stock. And rightly so it will print like this.
        print "Current Price: %f" % (stock[2])
        ......
results:  Current Price: 70.700000      

What I want is for it to print at 70.70, but not be a string, but a float for
math purposes.
        
I have tried several ways and it won't error, but doesn't do what I want.

Tried:

        cp=stock[2]
        print round(cp,2)
        ......
results:    70.7    

Above is good and works, but: 
        
        cp=stock[2]
        print "Current Price: %f" % round(cp,2)
        .......
results:   Current Price: 70.700000

Same here:

        cp=round(stock[2],2)
        print "Current Price: %f" % (cp)        
        .......
results:   Current Price: 70.700000


I don't have a problem storing the number AS IS in the database, but would like it to work
the way I thought it should.  Maybe the formatting will work better when I pull it from the database
and not from the list. Then again, maybe I am missing something here.

Any help??

Thanks.
Dave