When I divide 317 by 219 in Java using doubles I get 1.
For example:
double b = 317/219;
System.out.println(b);
Output is: 1.
Is this because it is a recurring decimal?
Have had to use BigDecimal instead which is annoying.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Try this
The default type of coded numbers in java is
int, so with the code as you have it java is working with twointnumbers and the result of the division would then beinttoo, which will truncate the decimal part to give a final result of1. Thisintresult is then cast fromint 1to adouble 1without a compiler warning because it’s a widening cast (one where the source type is guaranteed to “fit” into the target type).By coding either of the numbers as
doublewith the trailingD(you may also used, but I always use upper case letters becauseLas lowercasellooks like a1), the result of the division will bedoubletoo.