Solved Double decimal place value issue

Discussion in 'Plugin Development' started by Artellet, Sep 12, 2018.

  1. Offline

    Artellet

    *snip*
     
    Last edited: Sep 14, 2018
  2. Online

    timtower Ninja on the waves Moderator

    @Artellet What is the goal of this function? You could multiply it by 100, Math.floor it, then divide by 100 again.
     
  3. Offline

    Artellet

    *snip*
     
    Last edited: Sep 14, 2018
  4. Online

    timtower Ninja on the waves Moderator

    Let me know if it is what you needed ;)
     
  5. Offline

    Artellet

    *snip*
     
    Last edited: Sep 14, 2018
  6. Offline

    sebcio98

    Why not use int or long for everything and when you want to show the number to the player cast to double and divide by 100 (well, depends on how many digits after the decimal point you want)?
     
  7. Offline

    Artellet

    *snip*
     
    Last edited: Sep 14, 2018
  8. Offline

    sebcio98

    ...
    eh, I guess I need to explain in more detail.

    No, I didn't misunderstand the thread. If you keep all of the money in an integer - let's say in cents - then all of the operations you do on them, including withdrawal, are also done on integers, completely erasing the problem you have. Cents are 1/100 of a dollar, so you don't need any fractions smaller than that when operating on money. If you multiply that by a 100, it leaves you with only integers.

    This system obviously supports any currency, as long as you decide on how many digits after the decimal point in the main currency you need.
     
    Zombie_Striker likes this.
  9. Offline

    Artellet

    *snip*
     
    Last edited: Sep 14, 2018
  10. @Artellet
    Exactly. Which is why you want to use it and then cast and divide by 100, so you only get a number with two decimal places.
     
  11. Offline

    Artellet

    *snip*

    Final edit: Solved thanks to @sebcio98 & @Zombie_Striker.

    Spoonfeed:
    Code:
    yourDouble = ((double) ((int) (yourDouble * 100))) / 100;
     
    Last edited: Sep 14, 2018

Share This Page