how to store a left hand data seperate and right hand data seperately from a given variable

Only required in java, also without using strings or any specific objects, (just use maths)

problem is

double a = 13.564;
//now i want  

int b = 13;
int c = 564;

>Solution :

Using String it would be easier as we don’t need to know how many decimal there is

double a = 13.564;

String[] parts = Double.toString(a).split("\\.");

int b = Integer.parseInt(parts[0]);
int c = Integer.parseInt(parts[1]);
System.out.println(b + "/" + c);

With math only, you need to be careful so, a way is so multiply the number so there is no decimal part, the retrieve what was the decimal part

double a = 13.564;

int b = (int) a;

double power = Math.pow(10, BigDecimal.valueOf(a).scale());
int c = (int) (a * power - b * power);
System.out.println(b + "/" + c);

Leave a Reply