Solved

Round down a float

Posted on 2011-09-23
3
447 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
[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
  • Learn & ask questions
  • 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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 …
This article will show, step by step, how to integrate R code into a R Sweave document
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

623 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