3

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

我有

    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

} 

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

亲切地

4

7 回答 7

11

为什么不简单地使用:

int anyMethod(double a){
  //if the number has two digits after the decimal point.
  return (int)((a + 0.001) * 100) % 100;
}
于 2013-03-16T16:27:09.760 回答
8

获取 a 的小数部分的一种简单方法double是使用模运算符%. 但是,您必须考虑浮点运算不精确的事实。例如,

System.out.println(12.1 % 1);   // outputs 0.09999999999999964
System.out.println(12.99 % 1);  // outputs 0.9900000000000002

如果你想得到两个十进制数字int,这就是我认为你要问的,你可以做到这一点,掩盖浮点问题,如下所示:

System.out.println(Math.round((12.1 % 1) * 100));   // outputs 10
System.out.println(Math.round((12.99 % 1) * 100));  // outputs 99

但是,您应该考虑BigDecimal沿着您开始的路径走得更远,它使用任意精度算术。你可以这样做:

System.out.println(new BigDecimal("12.1").remainder(BigDecimal.ONE));   // outputs 0.1
System.out.println(new BigDecimal("12.99").remainder(BigDecimal.ONE));   // outputs 0.99

如果像以前一样,您想要两个十进制数字,您可以这样做:

System.out.println(new BigDecimal("12.1").remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).intValue());   // outputs 0.1
System.out.println(new BigDecimal("12.99").remainder(BigDecimal.ONE).multiply(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).intValue());   // outputs 0.99

Note that there a couple of differences between these last two methods and the first two: they preserve the sign of the argument, so if you use the final example for -12.99, you'll get -99 back, and they treat the fractional part of an integer as 1, so if you use the final example for 12, you'll get 100 back.

于 2013-03-16T16:54:42.133 回答
3

没有必要一直使用Number类型。您可以利用String作为调解员。

String mediator = Double.valueOf(d1).toString();
mediator = mediator.substring(mediator.indexOf('.') + 1);
Integer result = Integer.valueOf(mediator);
于 2013-03-16T16:25:30.310 回答
1

试试这个

 public static void main(String args[]){
        double a=12.99;
        double b=12.1;


        System.out.println(method(a));
        System.out.println(method(b));
    }

    private static int method(double a) {
        return (int) ((a*100)%100);

    }    
于 2013-03-16T16:21:21.877 回答
0

n如果我理解正确,您需要在给定数字的点之后返回数字double。所以......让我们看看:

public int decimalDigits(double x, int n) {
    double ans;
    ans = (x - (int) x) * Math.pow(10, n);
    return (int) ans;
}

完毕。希望这对您有所帮助。


对于您的具体示例, 'n = 2' 应该可以。

于 2013-03-16T16:25:23.590 回答
0

sachin-pasalkar 做到了!小修复,但很好!

public static int anyMethod(double a){
      return (int) (a*100)%100;
    }
于 2013-03-16T16:37:24.487 回答
0

查看此代码在“。”之后返回数字 总是。除了双变量之外没有任何额外的参数。

public int anyMethod(double d)
{
    String numString = d+"";
    return Integer.parseInt(numString.substring(numString.indexOf('.')+1));
}
于 2013-03-16T16:50:11.693 回答