简体   繁体   中英

Java Double get all Digits after dot/comma

it´sa simple task but i´m not able to solve it on my own..

i got

    double digit1 = 12.1;
    double digit2 = 12.99;

and need a method which gives me this:

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

what i have is

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

} 

anyway i prefer integer as the returntype.. anybody got a quick solution/tip?

kindly

Why not you simply use:

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

A simple way of getting the fractional part of a double is to use the modulo operator, % . However, you'll have to take into account the fact that floating point arithmetic isn't precise. For example,

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

If you want to get two decimal digits as an int , which is what I think you're asking, you can achieve this, glossing over the floating point issues, like so:

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

However, you should consider going further down the BigDecimal path you started down, which uses arbitrary precision arithmetic. You could do something like this:

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

If, as before, you want two decimal digits from this, you can do this:

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.

It's not necessary to use Number tyeps all the time. You can take advantage of String as a mediator.

String mediator = Double.valueOf(d1).toString();
mediator = mediator.substring(mediator.indexOf('.') + 1);
Integer result = Integer.valueOf(mediator);

Try this out

 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);

    }    

If I understand correctly, you need to return n digits after the dot for a given double number. So... let's see:

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

Done. Hope this helps you.


For your specific example, 'n = 2' should do.

sachin-pasalkar done it! little fix but fine!

public static int anyMethod(double a){
      return (int) (a*100)%100;
    }

check out this code returns digits after '.' always. Without any extra parameters other than double variable.

public int anyMethod(double d)
{
    String numString = d+"";
    return Integer.parseInt(numString.substring(numString.indexOf('.')+1));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM