Solved Double decimal place value issue

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

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

    Artellet

    I'm trying to make a boolean that checks whether or not a value has more than two place values.

    Code:
        public Boolean isValidDecimal(final double number) {
            return String.valueOf(number).split("\\.")[1].length() < 3; }
    
    However, I'm running into an issue with 8 or more digits. It's detecting that there's a place value > 3 when there are no periods in the number and I have no idea why.

    For example, 1.99 will return true with my method, whereas 1.999 would return false.
    A value with 8 digits (or more) will make it return false, for example; 9999999 will not but 99999999 will.

    I've tried formatting the number using NumberFormat.getInstance().format() with no success. I don't want to round the value as it's imperative that the value is as the user specifies.

    Any ideas?
     
    Last edited: Sep 13, 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

    It's used for withdrawal purposes (e.g money withdrawals). Thank you, I'll give that a shot now.
     
    Last edited: Sep 12, 2018
  4. Online

    timtower Ninja on the waves Moderator

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

    Artellet

    Unfortunately, that's not what I was looking for.

    Say the user has exactly $6 in their economy, the user withdraws $5.001, the economy will display $0.99 even though the third place value is never displayed or used in other aspects. Not to mention it's not possible with most if not all currencies (specifically the one I'm using) to get a value like that ( 0.00).

    I'm checking it on a command level to just return if the number specified has three or more place values.
     
    Last edited: Sep 12, 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

    Perhaps you misunderstood this entire thread, it's used for a money withdrawal system. I need it to support cents, not just display them. It needs to be a double, float makes no difference.
     
  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

    I don't understand what you're getting at. As far as I know integers don't accept values like 0.xx?
     
  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

    Alright, I'll see what I come up with.

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

    Spoonfeed:
    Code:
    yourDouble = ((double) ((int) (yourDouble * 100))) / 100;
     
    Last edited by a moderator: Oct 9, 2018
Thread Status:
Not open for further replies.

Share This Page