Solved

Round down a float

Posted on 2011-09-23
3
442 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-
Comment Utility
x = 15.055
print("%.2f" % (floor(x*100)/100))
0
 
LVL 5

Accepted Solution

by:
-Richard- earned 250 total points
Comment Utility
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
Comment Utility
looks great to me!
Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now