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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With