Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Round down a float

Posted on 2011-09-23
Medium Priority
448 Views
Hi all,
I tried using print("%.2f" % 15.055) but the output rounded up to 15.06
How should I do to get an output of 15.05 instead of 15.06?
0
Question by:crazy4s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2

LVL 5

Expert Comment

ID: 36590479
x = 15.055
print("%.2f" % (floor(x*100)/100))
0

LVL 5

Accepted Solution

-Richard- earned 1000 total points
ID: 36590497
Basically, what you want is a number rounded down with two decimal places.  Multiplying X by 100, taking the floor, and dividing by 100, accomplishes that.  If youwant 3 decimal places, then use 1000; four, 10000; etc.

There is probably a more elegant way to accomplish this, but this method is mathematically correct and it's not hard, so what the heck!   :-)
0

Author Comment

ID: 36590572
looks great to me!
Thanks!
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month8 days, 3 hours left to enroll