Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Round down a float

Posted on 2011-09-23
3
Medium Priority
?
450 Views
Last Modified: 2012-05-12
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
Comment
Question by:crazy4s
  • 2
3 Comments
 
LVL 5

Expert Comment

by:-Richard-
ID: 36590479
x = 15.055
print("%.2f" % (floor(x*100)/100))
0
 
LVL 5

Accepted Solution

by:
-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

by:crazy4s
ID: 36590572
looks great to me!
Thanks!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question