How to get decimal parts in integer from double?

Yaroslav Nudnenko :

How to separate double into two integer number? First number before and second number after decimal point. For example:

            double doub = 543.345671;
            int num1 = (int) doub; //543
            int num2 = getNumAfterDecimal(doub-num1); //return 345671

I need decimal part to integer.

Nir Levy :

It depends how many digits you want after the decimal point, but this is the gist:

double d = doub - (int)doub; // will give you 0.xyz
int result = d * 1000; // this is for 3 digits, the number of zeros in the multiplier decides the number of digits 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85244&siteId=1