To round to the nearest of any value
int round(double i, int v){
return Math.round(i/v) * v;
}
You can also replace Math.round()
with either Math.floor()
or Math.ceil()
to make it always round down or always round up.
More Related Contents:
- Round a double to 2 decimal places [duplicate]
- How to resolve a Java Rounding Double issue [duplicate]
- How to round a number to n decimal places in Java
- Why does Math.round(0.49999999999999994) return 1?
- Java Round up Any Number
- Rounding BigDecimal to *always* have two decimal places
- Rounding a double to turn it into an int (java)
- Java BigDecimal: Round to the nearest whole value
- Converting double to integer in Java
- Java round to nearest .5 [duplicate]
- How many decimal Places in A Double (Java)
- Is inconsistency in rounding between Java 7 and Java 8 a bug?
- round BigDecimal to nearest 5 cents
- System.currentTimeMillis vs System.nanoTime
- What components are MVC in JSF MVC framework?
- Java NIO FileChannel versus FileOutputstream performance / usefulness
- Partition a Java 8 Stream
- Convert from List to CompletableFuture
- Choose between ExecutorService’s submit and ExecutorService’s execute
- Getting the inputstream from a classpath resource (XML file)
- Is there a good reason to use “printf” instead of “print” in java?
- Malicious code vulnerability – May expose internal representation by incorporating reference to mutable object
- WebDriverException: unknown error: Runtime.executionContextCreated has invalid ‘context’: while initializing Chrome browser
- Read String line by line
- How to get the last characters in a String in Java, regardless of String size
- Android – Bitmap cache takes a lot of memory
- Choose the series of data that you want to display
- How to replace � in a string
- Running javafx sample on JDK 11 with OpenJFX 11 JMODS on Module Path
- JPA Updating Bidirectional Association