mathintegernumbersroundingnumber-formatting# Round up the last digit of whole numbers

I have a number on which I need to round up the last digit, either to 0 or 5.

Examples:

- From
`0, 1, 2`

to`0`

, for example 12 -> 10 - From
`3, 4, 5, 6, 7`

to`5`

, like 37 -> 35 - And from
`8, 9, 0`

to`0`

, again like 59 -> 60

I tried `Math.ceil()`

or `Math.floor()`

but those are not working. As I've found out those are for floating point numbers and can not be used for this problem.

Solution

`Math.ceil`

and `Math.floor`

are for floating-point numbers (like `Double`

s). There's no built-in function for what you want to do, but this should work

```
public static int round(int original) {
if original % 10 <= 2 {
return original - (original % 10)
} else if original % 10 <= 7 {
return original - (original % 10) + 5
}
return original - (original % 10) + 10
}
```

If you want it to work with negative numbers, use this:

```
public static int round(int original) {
if original % 10 <= 2 {
return original - (original % 10)
} else if original % 10 <= 7 {
return original - (original % 10) + 5
} else if original % 10 <= 9 {
return original - (original % 10) + 10
} else if original % 10 >= -2 {
return original - (original % 10)
} else if original % 10 >= -7 {
return original - (original % 10) - 5
}
return original - (original % 10) - 10
}
```

Clearly, you can't just copy & paste this code, because you'll need to put it in a class and possibly change the visibility and `static`

ness (the `public static`

) part.

To use it, call `int rounded = round(input)`

Hope this helps!

- MATLAB: Using ODE solvers?
- Runge-Kutta (RK4) integration for game physics
- Fast algorithm for multiple line integrals over 2D discrete function
- how to display math equations (integration, algebraic, etc) in UILabel
- Integrating a Bayes probability distribution
- Evaluate limit of integral in Mathematica
- JavaScript % (modulo) gives a negative result for negative numbers
- Truncate a floating point number without rounding up
- Resize div with drag handle when rotated
- Calculation of intersections between line segments
- Identity in sympy
- solution for basic integration of mathematical functions
- Why does my Runge-Kutta implementation oscillate to 0?
- Integration via trapezoidal sums in MATLAB
- stieltjes integration in Python
- C++ nan keeps coming up with integration
- integration 3 dimensions by generalizing the method of rectangle
- How to solve integral in Matlab having an upper limit set as the sine function?
- Trouble using Sympy to integrate
- Obtaining the constant that makes the integral equal to zero in Matlab
- Determining the event location on an oscillatory signal
- Code wont produce the value of a definite integral in MATLAB
- Triple Integration: ValueError: negative number cannot be raised to a fractional power
- Scipy tplquad syntax
- implement an integration math equation using odeint in Python
- How to do this integral using tplquad, dblquad in python?
- How to always get the same number from a changable number?
- Evaluating integral using Riemann sums
- How to use `scipy.integrate.quad` to compute integral of a function which depends on the integral of another function
- Polynomial which satisfies integral and two points