Solved Problem with some math

Discussion in 'Plugin Development' started by Catchet, Oct 14, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Catchet

    Hello! I made this little code for calculating how much "money" a user has, but instead of saying just: "You have 999999999 money", I want it to say "You have 9d(diamonds), 99e(emeralds), 99g(gold), 99s(silver) and 99c(copper)"
    1d = 100e, 1e = 100g, 1g = 100s and 1s =100c.
    Here's the code for it:
    Code:java
    1. double diamondDec = copperAmount / 100000000;
    2. double emeraldDec = copperAmount / 1000000 - diamondDec * 100;
    3. double goldDec = copperAmount / 10000 - (emeraldDec * 100 + diamondDec * 100);
    4. double silverDec = copperAmount / 100 - (goldDec * 100 + emeraldDec * 100 + diamondDec * 100);
    5. double copperDec = copperAmount - (silverDec * 100 + goldDec * 100 + emeraldDec * 100 + diamondDec * 100);

    Where "copperAmount" equals the total amount of money the player has.

    If the player has between 0 and 9999 money it is displayed as it should.
    But if it is over that it all becomes very weird...
    For example if the player has 12345 money it is displayed as: 1g, 23s and 9945c.
    So it adds 9900 to the number.
    And if the number is 1234567 it becomes 1e, 23g, 9945s and 237667c.
     
  2. Catchet correct me if I am wrong, but you can't subtract an int from a double right?
     
  3. Offline

    Appljuze

    You should be using all integers. You're gonna get problems and lose digits if you cast every operation back to an integer (and every digit matters in this case). You won't need to manage decimals because you won't have any, correct? Unless a player can have .5 of a copper (which would be stupid in my opinion).

    I didn't take the time to figure this math out, but from what I'm looking at, you're going about it the wrong way. Just dividing by 100 isn't going to give you the format you want (I think). I believe the modulus operator would be useful in this case
     
  4. Offline

    Catchet

    Datdenkikniet I think that's possible, because
    Code:java
    1. double a = 2.5;
    2. int b = 2;
    3. double c = a - b;
    4. Bukkit.broadcastMessage("" + c);

    returns 0.5

    Appljuze
    That code is giving me the right format (as I mentioned) but it gets messed up with large numbers.

    How would the modulus operator help me here do you mean?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  5. Offline

    Appljuze

    Catchet
    For example, if they have 856 copper, and you run the modulus on it by 100, it will return 56. Then you do the original number minus the number it returned to get how many silver. So lets see...
    856 % 100 = 56
    856 - 56 = 800
    800 / 100 = 8 silver
    So they have 8 silver and 56 copper
    Now lets try with something bigger.
    156653 % 100 = 53
    156653 - 53 = 156600
    156600 / 100 = 1566 silver
    So they have 1566 silver and 53 copper. But 1566 is too much silver, so we repeat.
    1566 % 100 = 66 silver
    1566 - 66 = 1500
    1500 / 100 = 15 gold
    Now we're good. So they have 15 gold, 66 silver and 53 copper.
     
    Catchet likes this.
  6. Offline

    Catchet

    Appljuze
    Oh, thank you, I'm used to using the modulus in if statements, so I forgot that you could actually use it anywhere else.

    Solved!

    EDIT: I also thought that it (%) would return how many decimals it would be, not what the actual decimals would be
     
  7. Offline

    Appljuze

    It doesn't return what the decimals would be. The modulus returns the remainder of division between the two numbers you give it. So for example, 9 % 2 would be 1.
    Because 9 / 2 = 8 with 1 left over. If it's an even division, the modulus will be 0. Like if it's 10 % 2, that will be 0 because 2 goes into 10 evenly.
     
    Catchet likes this.
  8. Offline

    Catchet

    Appljuze
    That explains why 10 % 3 = 1...
    I'm feeling dumb now.
    But anyway, thanks for informing me
     
Thread Status:
Not open for further replies.

Share This Page