-1
int number = 24.24;

int afterDot = (int) (number*100)%100;
afterDot = 24

This logic is wrong if

int number1 = 24.4 

How can I get the 4 from 24.4 ?
Whatever will be the number but want to extract the value after dot. Actually the formula has to work for both type of value.

khelwood
  • 52,115
  • 13
  • 74
  • 94
namrata shahade
  • 173
  • 1
  • 1
  • 9

1 Answers1

1

If you convert your double to a string, the problem becomes easier :

double number = 24.4;
String numberAsString = String.valueOf(number);
String decimalPart = numberAsString.split("\\.")[1];
System.out.println(decimalPart);
int number1 = Integer.valueOf(decimalPart); // NOTE: This conversion is lossy.
System.out.println(number1);

Note that by converting your decimalPart string (e.g. "001") to an integer (1), you might lose some information.

With 24.4, it outputs :

4
4

With 24.001, it outputs :

001
1

With 3d, it outputs:

0
0
Eric Duminil
  • 50,694
  • 8
  • 64
  • 113