这是我的程序。非常简单地说,它应该将输入的数字四舍五入到给定的小数位数。即 4.245 四舍五入到 2 位小数将得到 4.25。但是,我总是只把它四舍五入到一个整数后面......为什么?让我感到困惑的是,我在 android 中使用了相同的代码,而且它完美无缺
import java.util.Scanner;
public class Rounder {
public static void main(String[] args) {
double input1, roundednumber;
int input2;
Scanner keyboard = new Scanner(System.in);
System.out.println("number to round ");
input1 = keyboard.nextDouble();
System.out.println("decimals");
input2 = keyboard.nextInt();
roundednumber = rounder(input1, input2);
System.out.println(roundednumber);
}
public static double rounder(double number, int decimals){
int multiplier = (int) Math.pow(10, decimals);
double roundednumber;
roundednumber = Math.round(number*multiplier)/multiplier;
return roundednumber;
}
}
这是我的android类的片段
double result1 = fv1/(Math.pow(1+(r1/n1),n1*t1));
result1 = (double) (Math.round(result1 * 100)) / 100;