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

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -