Convert from float to integer

129 Views Asked by At

This may seem like a stupid question but

What method can one use to convert a decimal number, such as $0.672$ into a whole number?

This isn't rounding as $0.999$ should result in $0$, but saying "floor the number" isn't an answer as I'm looking for a mathematical method in which a number, $n$, can be entered and would produce the same result as $floor(n)$.

For those familiar with programming, this is akin to the int function in many languages.

As asked in the comments, when given $-0.5$ the result should be $-1$ as it rounds to negative infinity.

Clarification: This is not about "what calculator can do what", this is about a mathematical question about a method of calculation. I used a calculator to prevent the smart-arse answers I usually get when I ask questions, not as something you can base your answer on.

2

There are 2 best solutions below

1
On

As a programmer, given a float called f, what I would do is

int floor( float f ) {
    float r = f % 1;
    return = f - r;
}

In English, you find the portion that is less than 1 by doing a modulus operation, and then subtract that amount. This only works with positive numbers; not sure if this works on negative numbers, and if it doesn't, how to handle them. The function above should probably check for values less than zero and return and error.

0
On

I'm afraid you're out of luck. The function you want is just plain floor, defined by $$ \text{floor}(x) = \text{ greatest integer less than or equal to } x $$

There is no other

mathematical method in which a number, $n$, can be entered and would produce the same result as $\text{floor}(n)$.

As @scott points out in his answer, you can compute the floor if you extend the definition of the modulus operator as some computer languages do. But that's essentially just wrapping the floor function.