Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: print()

Reply
Thread Tools

Re: print()

 
 
Lie Ryan
Guest
Posts: n/a
 
      10-17-2009
mattia wrote:
> Another question (always py3). How can I print only the first number
> after the comma of a division?
> e.g. print(8/3) --> 2.66666666667
> I just want 2.6 (or 2.66)


Are you sure you don't want that to be 2.7 or 2.67? Then you can use:
n = int(n * 10**2) / 10**2
else if 2.7 pr 2.67 is what you wanted, you could use:
n = round(n, 2)
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      10-17-2009
Lie Ryan wrote:
> mattia wrote:
>> Another question (always py3). How can I print only the first number
>> after the comma of a division?
>> e.g. print(8/3) --> 2.66666666667
>> I just want 2.6 (or 2.66)

>
> Are you sure you don't want that to be 2.7 or 2.67? Then you can use:
> n = int(n * 10**2) / 10**2
> else if 2.7 pr 2.67 is what you wanted, you could use:
> n = round(n, 2)
>
>

Bad idea to use round() to make numbers format properly. No guarantees
that the print logic will then print the number rounded the way you
want. It could just as easily do 2.69999999999999 when you finally go
to print it.

Best to use format(), the way it was intended. Round to decimal while
converting to decimal. Otherwise surprises await in dark corners.

DaveA
 
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




Advertisments