Java double gets all numbers after dot / comma
•
Java
This is a simple task, but I can't solve it myself
I have
double digit1 = 12.1; double digit2 = 12.99;
And need a way to give me this:
anyMethod(digit1); //returns 10 anyMethod(digit2); //returns 99
What do I have
public static void getAfterComma(double digit) { BigDecimal bd = new BigDecimal(( digit - Math.floor( digit )) * 100 ); bd = bd.setScale(4,RoundingMode.HALF_DOWN); System.out.println(bd.toBigInteger()); // prints digit1=1 and digit2=99 }
Anyway, I prefer integer as the return type Did anyone get a quick solution / tip?
Amiable
Solution
Why not simply use:
int anyMethod(double a){ //if the number has two digits after the decimal point. return (int)((a + 0.001) * 100) % 100; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码