casting - Convert decimal part of a double to integer? in JAVA -
i want integer being follows:
double number= 9.7361;
and want intenger have this: 7361.
another example:
double number2= 0.43;
and want integer have this: 43.
in java.
your question has answer so long as limit how far fractional part of double can go. modified question, consider following function:
public static int fracpartasint(double d, int digits){ return (int)((d - ((int)d)) * math.pow(10, digits)); } first isolate fractional part of double:
(d - ((int)d) then give digits digits multiplying 10^digits
(d - ((int)d)) * math.pow(10, digits) finally discard rest truncating int.
Comments
Post a Comment