user1621988
user1621988

Reputation: 4335

Double -> Extract Whole Numbers

int a = 4;
int b = 3;
int c = 10;
int d1 =(int) (double)(a*b)/c;
double d2 =(double)(a*b)/c;
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);

Result: d1: 1 and d2: 1.2

How do extract/remove the 1.0 of 1.2. So i get d2 = 0.2 and d1 = 1 And when a = 9 -> (9*3)/10. d2 = 0.7 and d1 = 2 And when a = 6 -> (6*3)/10. d2 = 0.8 and d1 = 1

Thanks a lot.

Upvotes: 0

Views: 297

Answers (3)

mssb
mssb

Reputation: 878

Try this

int a = 9;
int b = 3;
int c = 10;

int d1 =(int) (double)(a*b)/c;
double d2 =(double)((a*b)%c)/c;
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612954

Simply subtract the integer part from the floating point value:

double d = (double) (a*b)/c;
int intPart = (int) d;
double fracPart = d - intPart;

Upvotes: 1

Vinnyq12
Vinnyq12

Reputation: 1559

int a = 4;
int b = 3;
int c = 10;
// Store the original value.
double original = (double)(a*b)/c;
int d1 = (int)(original);
// Get the difference between the original value and the floored one.
double d2 = original - d1;
System.out.println("d1: " + d1);
System.out.println("d2: " + d2);

Upvotes: 2

Related Questions