简体   繁体   中英

Android java using Eclipse math, I don't know why simple math is coming up with wrong result?

I have four variables with the following values (I am sure about the values they have):

15
100
1
4000

I am trying to output the values into a toast message, but they are coming up wrong.

Here is what I did:

Toast.makeText( TreActivity.this,
                String.valueOf( ((15 + 100) * 10) + 4000 ),
                Toast.LENGTH_SHORT ).show();

When testing this in the emulator, it comes up as 14150 . This is wrong, because ((15+100)*10)+4000 = 5150.

I must be missing something special with Android/Java in the way things are calculated.

Any help is much appreciated!

Dont do String.valueOf(...) just try your calculation and then at the end put +"" to make it a string. See what that does

UPDATE:

If the problem isnt fixed yet you should try breaking the equasion down to see where the issue is. you should add 100 and 15 together and just put 150 and then just keep doing that untill you see where the issue is. It might be that you have a parenthases out of place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM