4

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.

Johnny Chen
  • 2,025
  • 1
  • 18
  • 27

10 Answers10

16

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
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
4

You could treat the double like a string, and split it on the decimal place.

     double d = 13454.92345;
     String bob = Double.toString(d);
     String[] convert = bob.split("\\.");

     int a = Integer.parseInt(convert[0]);
     int b = Integer.parseInt(convert[1]);

     System.out.println(a); // 13454
     System.out.println(b); // 92345
JamesA
  • 365
  • 3
  • 11
2

To get the decimal part as an int is not really following any standards, so you are stuck with your special solution for that. Getting the value before the decimal point can be simplified to a simple cast though:

double d = 1.9;
int a, b;
String dString = Double.toString(d);
String bString = dString.substring(2);
a = (int) d;
b = Integer.parseInt(bString);

Note that the substring() and parseInt() fails if the number is 10 or bigger though. You might want to change the substring() call to something like:

String bString = dString.split("\\.")[1];
Keppil
  • 45,603
  • 8
  • 97
  • 119
1
double num=12.5;
String str=Double.toString(num);
String strarray[]=str.split("\\.");

Now strarray[0] will be holding 12, and strarray[1] will be having 5.You can convert them into integers by writing following code:

int num1=Integer.parseInt(strarray[0]);
int num2=Integer.parseInt(strarray[1]);
nobalG
  • 4,544
  • 3
  • 34
  • 72
1

Here's a two line version:

int a = (int)d;
int b = Integer.parseInt((d + "").split("\\.", 2)[1]).replaceAll("^$", "0"));

The first part is easy - just cast to int, which automatically chops off any decimal part of the number.

The second part is easiest with a Strimg approach - split the string version on a dot and use the second part. Note the addition of "" which generates a string, and the handling of the edge case of there being no decimal part, where 3 as a double prints as 3. - if the result is a blank (guaranteed by the second parameter to split) which is then converted to a zero.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
1
  double d = 1.9;
  String str = Double.toString(d);
  String strArray[] = str.split("\\.");

  int a = Integer.parseInt(strArray[0]);
  int b = Integer.parseInt(strArray[1]);
  System.out.print(a + " - " + b);

Try this. This will work.

Andrey
  • 6,526
  • 3
  • 39
  • 58
bumbumpaw
  • 2,522
  • 1
  • 24
  • 54
0

This solution should work for almost any length of the fractional part and doesn't use strings. For example the accepted answer doesn't work for 0.16

double d = 123.456

long a = (long) d;
double f = d - a;

while (Math.abs((long) f - f) > 0.000001) f *= 10;

long b = (long) f;

// a = 123
// b = 345
Patrik Oldsberg
  • 1,540
  • 12
  • 14
0

Here is another approach:

double d=1.9;
int a;
int b;

a = (int) d/10;
b = (int) d%10;

System.out.print("a");
System.out.print("b");
Richard Muñoz
  • 187
  • 3
  • 12
0
    double dub=1234.5678987;//Variable to be manipulated
    long lon=0; //To store before decimal point.    
    short sh=0;//To store after decimal point.  
    int deci=10000; //to 4 decimal places   
    lon=(int)dub;// get the integer part of the number stored in long   
    sh=(short)((dub-(double)lon)*deci);//deci to required decimal places.
    System.out.println(""+lon+"."+sh);
Loyola
  • 1
  • 1
-1

I tried a split on "." and then just parse the string for integers.

        String a="";
        double x = 2.4;
        a=x+"";
        String [] v =a.split("\\.");
        int before = Integer.parseInt(v[0]);
        int after = Integer.parseInt(v[1]);
tudoricc
  • 709
  • 1
  • 12
  • 31