gpt4 book ai didi

Java Double获取点/逗号后的所有数字

转载 作者:搜寻专家 更新时间:2023-11-01 03:59:11 25 4
gpt4 key购买 nike

这是一个简单的任务,但我无法独自解决..

我得到了

    double digit1 = 12.1;
double digit2 = 12.99;

并且需要一个方法给我这个:

    anyMethod(digit1); //returns 10
anyMethod(digit2); //returns 99

我有的是

public static void getAfterComma(double digit) {

BigDecimal bd = new BigDecimal(( digit - Math.floor( digit )) * 100 );
bd = bd.setScale(4,RoundingMode.HALF_DOWN);
System.out.println(bd.toBigInteger()); // prints digit1=1 and digit2=99

}

无论如何我更喜欢整数作为返回类型..有人有快速解决方案/提示吗?

亲切的

最佳答案

为什么不直接使用:

int anyMethod(double a){
//if the number has two digits after the decimal point.
return (int)((a + 0.001) * 100) % 100;
}

关于Java Double获取点/逗号后的所有数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15451492/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com