Java rounded so that Java retains 2 decimal places for example
- 2020-04-01 02:31:10
- OfStack
package com.icer.test;
public class MyRound {
public static void main(String[] args) {
double num = 3.23562;
double number = 0;
number = new MyRound().myRound(num,2);
System.out.println("after:" + number);
}
private double myRound(double number,int index){
double result = 0;
double temp = Math.pow(10, index);
result = Math.round(number*temp)/temp;
return result;
}
}
Because the round function in Java removes the decimal place after processing the small number, it multiplies 100 and then divides by 100.0.
Make sure the output is a decimal number. Otherwise it will be truncated to an integer