Solved

Round down a float

Posted on 2011-09-23
3
445 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 250 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
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…
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…

749 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