2

I am developing an app in which i have to get the float part in terms of integer value.

for example if my number is 153.12324 then output should be 12324.I tried this way but it works wrong some time.

What's the problem in this or is there a better way to do this ?

there is a double value in d

    double d =any_double_value;
    String[] splitter = String.valueOf(d).split("\\.");
    splitter[0].length();   // Before Decimal Count
    int count = splitter[1].length();
    int integerpart = (int) calcResult;
    double floatpart = calcResult - integerpart;
    while (count!=0)
    {
        floatpart=floatpart*10;
        count--;
    }

    int floatpartinInt=(int)floatpart;

this works wrong only in some cases(it gives 1 number less, like if answer is 124 it gives 123) and in cases where answer is long double no like 3.333333333 (10/3)

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
Manohar
  • 22,116
  • 9
  • 108
  • 144

3 Answers3

3

You can also without split. by using following way,

double value = 3.25;
double fractionalPart = value % 1;
double integralPart = value - fractionalPart;

This may helps you

Sathish Kumar J
  • 4,280
  • 1
  • 20
  • 48
0

Use this code its work in your case and enjoy Man

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
Nitesh Pareek
  • 362
  • 2
  • 10
0

You could also do this:

lvalue = (long) value;
fPart = value - lvalue;
wake-0
  • 3,918
  • 5
  • 28
  • 45