0

I have a simple question, I want to know how can I get the decimal part from a double/float number without the dot. Example: a=0.75 and b=3231.0131 So I would like to set those decimal values in two new Integer variables: m=75 and b=0131.

I'm going to clarify some things, I want to create a new int variable, that variable will storage the decimal part from the original number.

double a = 0.75
double b = 12.033
int x = decimalofa
int y = decimalofb

System.out.println("the decimal of"+a+"is"+x+"and the decimal of"+b+"is"+y)
//the decimal of 0.75 is 75 and the decimal of 12.033 is 033

The thing is that i'm not sure if 033 could be considered as an integer number, so in other words I just want to take all the numbers next to the point and save them in a new variable.

Santiago
  • 3
  • 7
  • 2
    Just to clarify, you want to strip leading zeroes? So that 1.1, 1.01 and 1.001 would all become 1? – tgdavies May 07 '18 at 03:35
  • it seems weird what you want to do , in your example 131 would end up being a bigger number than 75, but it was a smaller number originally – Keith Nicholas May 07 '18 at 03:37
  • Think again about your requirements. Is this really what you want to do? – Henry May 07 '18 at 03:37
  • See this link https://stackoverflow.com/questions/24753177/how-to-split-a-double-number-by-dot-into-two-decimal-numbers-in-java – A J May 07 '18 at 03:39
  • I just want to get the decimal part of the number, so in the number 3231,0131 the decimal would be 0131 right? – Santiago May 07 '18 at 03:40
  • 1
    @Santiago no. When you represent it as an integer, it is 131. – Henry May 07 '18 at 03:40

1 Answers1

0

Just do

    float a = 0.75f;

    System.out.println(Float.toString(a).split("[.]")[1]);

This only works if there is a decimal and there are numbers after that decimal

ParryV
  • 33
  • 1
  • 4
  • If there are no decimals `ArrayIndexOutOfBoundsException` will be thrown. – Roshana Pitigala May 07 '18 at 03:53
  • @RoshanaPitigala yes, that's why I said in the answer this only works if there is a decimal and there are numbers after it – ParryV May 07 '18 at 04:00
  • No it won't give an `ArrayIndexOutOfBoundsException` even when there are no decimals. Because a `float` will always have a decimal point(`0.0`) even its not mentioned. My mistake! – Roshana Pitigala May 07 '18 at 04:04