Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a double number by dot into two decimal numbers in Java?

Tags:

java

Trying to split a double number into two decimal parts by dot. Like this: 1.9 into 1 and 9; 0.16 into 0 and 16;

Here's what I do, but seems a little redundant, what's the best way to do this?

The origin number will always be like Just 0.x or 1.x or 0.xx or 1.xx and xx > 10

    double d = 1.9;
    int a, b;
    String dString = Double.toString(d);
    String aString = dString.substring(0, 1);

    String bString = dString.substring(2);
    a = Integer.parseInt(aString);
    b = Integer.parseInt(bString);

My way of doing this seems using to much String conversion,which I don't think is very efficient.

like image 732
Johnny Chen Avatar asked Nov 29 '22 11:11

Johnny Chen


1 Answers

You can try this way too

    double val=1.9;
    String[] arr=String.valueOf(val).split("\\.");
    int[] intArr=new int[2];
    intArr[0]=Integer.parseInt(arr[0]); // 1
    intArr[1]=Integer.parseInt(arr[1]); // 9
like image 165
Ruchira Gayan Ranaweera Avatar answered Dec 11 '22 05:12

Ruchira Gayan Ranaweera