Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading