Questions tagged [floor]

A function common to many programming languages that returns the next lowest integer value by rounding the value down if necessary.

Returns the next lowest integer value by rounding down value if necessary. php.net

Examples

echo floor(1.3);    // returns   1
echo floor(10.9);   // returns  10
echo floor(-3.14);  // returns  -4
420 questions
123
votes
5 answers

Why does Math.Floor(Double) return a value of type Double?

I need to get the left hand side integer value from a decimal or double. For Ex: I need to get the value 4 from 4.6. I tried using Math.Floor function but it's returning a double value, for ex: It's returning 4.0 from 4.6. The MSDN documentation…
Sridhar
114
votes
5 answers

Floor or ceiling of a pandas series in python?

I have a pandas series series. If I want to get the element-wise floor or ceiling, is there a built in method or do I have to write the function and use apply? I ask because the data is big so I appreciate efficiency. Also this question has not…
wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
84
votes
5 answers

Why do lots of (old) programs use floor(0.5 + input) instead of round(input)?

The differences reside in the returned value giving inputs around tie-breaking I believe, such as this code: int main() { std::cout.precision(100); double input = std::nextafter(0.05, 0.0) / 0.1; double x1 = floor(0.5 + input); …
markzzz
  • 47,390
  • 120
  • 299
  • 507
61
votes
19 answers

Round minute down to nearest quarter hour

I need to round times down to the nearest quarter hour in PHP. The times are being pulled from a MySQL database from a datetime column and formatted like 2010-03-18 10:50:00. Example: 10:50 needs to be 10:45 1:12 needs to be 1:00 3:28 needs to be…
Rob
  • 631
  • 1
  • 6
  • 5
58
votes
6 answers

Does casting to an int after std::floor guarantee the right result?

I'd like a floor function with the syntax int floor(double x); but std::floor returns a double. Is static_cast (std::floor(x)); guaranteed to give me the correct integer, or could I have an off-by-one problem? It seems to work, but I'd like…
Jesse Beder
  • 33,081
  • 21
  • 109
  • 146
48
votes
7 answers

Taking the floor of a float

I have found two ways of taking floors in Python: 3.1415 // 1 and import math math.floor(3.1415) The problem with the first approach is that it return a float (namely 3.0). The second approach feels clumsy and too long. Are there alternative…
Randomblue
  • 112,777
  • 145
  • 353
  • 547
46
votes
4 answers

Rounding to nearest fraction (half, quarter, etc.)

So, I need to create the following functions but my head can't think of any possibility in PHP without complicated math. Round always up to the nearest decimal (1.81 = 1.90, 1.89 = 1.90, 1.85 = 1.90) Round always down to the nearest decimal (1.81 =…
user1649331
34
votes
5 answers

Efficient integer floor function in C++

I want to define an efficient integer floor function, i.e. a conversion from float or double that performs truncation towards minus infinity. We can assume that the values are such that no integer overflow occurs. So far I have a few…
user1196549
34
votes
1 answer

How do I create a new Joda DateTime truncated to the last hour?

I am pulling timestamps from a file that I want to create a new DateTime for, but I want to create the DateTime at the floor of the hour (or any Joda Period will do). How Can I do this?
WolfmanDragon
  • 7,851
  • 14
  • 49
  • 61
33
votes
1 answer

Ruby .ceil and .floor

I'm new to Ruby and I'm trying to figure out how ceil and floor works as I get different answers when a fraction or a decimal number is used (similar value). Below is what I have tried: puts 8/3.ceil == 2 #=> true puts 8/3.floor == 2 #=>…
misokuan
  • 333
  • 1
  • 3
  • 6
30
votes
3 answers

Getting the floor value of a number in SQLite?

I have a SQLite database with a table containing the scores of some league players in a bowling center. I'm trying to get the average of the Score column for each player ID. The problem with this is I only need the whole part of the average, and it…
Jumbala
  • 4,764
  • 9
  • 45
  • 65
23
votes
6 answers

How to ceil, floor and round bcmath numbers?

I need to mimic the exact functionality of the ceil(), floor() and round() functions on bcmath numbers, I've already found a very similar question but unfortunately the answer provided isn't good enough for me since it lacks support for negative…
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
22
votes
4 answers

floor and ceil with number of decimals

I need to floor a float number with an specific number of decimals. So: 2.1235 with 2 decimals --> 2.12 2.1276 with 2 decimals --> 2.12 (round would give 2.13 which is not what I need) The function np.round accepts a decimals parameter but it…
user2261062
22
votes
4 answers

C integer division and floor

In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes?
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
19
votes
6 answers

Does this mean that Java Math.floor is extremely slow?

I don't Java much. I am writing some optimized math code and I was shocked by my profiler results. My code collects values, interleaves the data and then chooses the values based on that. Java runs slower than my C++ and MATLAB implementations. I…
Mikhail
  • 7,749
  • 11
  • 62
  • 136
1
2 3
27 28