1

i'm new here and to Java Android Studio.

Currently i creating an App which functions as below :

  1. User key-in a digit for example 1131
  2. The App will take 1131 / 80 = 14.1375
  3. And the calculation will remove all the front digit which is the 14
  4. Use 0.1375 and Times 80
  5. Results = 11
  6. Then the App will display a certain message for the number 11

So far i'm stuck at the calculation which is For Example :

int x = 1131;
double x1 = x / 80.0;              //Gets 14.1375
double x2 = Math.floor(x1);        //Gets 14.0
double x3 = x1 - x2;               //14.1375 - 14.0 = 0.1374999999999993 ???

something weird shows up during the Math.floor, it suppose to take 14 and execute x1 - x2 ( 14.1375 - 14.0 = 0.1375 )

But instead it shows 0.1374999999999993

Thank you !!

Bellerofont
  • 1,081
  • 18
  • 17
  • 16
William
  • 11
  • 1
  • 2
    Possible duplicate of [Retain precision with double in Java](http://stackoverflow.com/questions/322749/retain-precision-with-double-in-java) – Matt Clark Jan 20 '17 at 15:31
  • Also [relevant](https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems). – Matt Clark Jan 20 '17 at 15:32
  • please add the complete code of what you have attempted in case to find the bug or problem it is necessary to review full code. – Hiren Jungi Jan 20 '17 at 15:33
  • Hey william, I'd like to note a few things. 1- Android Studio is not built as Java IDE, instead it's built for Android 2- This issue is related to computers in general due to the representation of numbers inside your computer. Try `0.1+0.2` in any programming language – Shady Atef Jan 20 '17 at 19:41
  • Shady, the app i creating is quite simple, the calculation is as per the 6 steps in the description. All i need to do now is create the formula for it only.---------- Hiren, the important part is the 4 lines of code i written there only. Once i get the calculation right then i'm good to go----------- Matt, noted with thanks but i have no idea how to use the BigDecimal into my formula =( still new to all these programming and apps thing – William Jan 21 '17 at 04:29

1 Answers1

0

This is because numbers in computer languages are represented by binary. See the answer posted here

Community
  • 1
  • 1
  • Hi Chris, i have go through the post but still no idea on how to add the BigDecimal into my formula. Math.floor seems cant be use with BigDecimal. Appreciate if you could assist on teach me how to write the formula code ? Thanks alot – William Jan 21 '17 at 04:23