Solved How make something a BigInteger from a config.

Discussion in 'Spigot Plugin Development' started by luwie556, Jan 23, 2018.

Thread Status:
Not open for further replies.
  1. Hello Spigot,
    I am coding a Prison Plugin which the rankup function is done and working well.

    I am running into an Issue in which I am confused.

    for something that is a HUGE number like 85000000000 (85 billion) it goes to -899 million.
    which i did research on BigIntegers which i tried to cast it to the getInt method for getting the rankup price said that you can't cast it to a getInt and there isnt a way to go and do getBigInt in config.

    Edit:
    Aswell as the other numbers that are big go to random numbers such as like 89657589686. same type of issue/resolution.

    any help/guidance?
     
  2. Using a Long should do the job. However, if you want to work with a number even bigger than 2^63-1, then you can just store it as a String. You'd then get the String from the config and pass it into the BigInteger constructor.
     
  3. If your numbers are going to go so high up use longs
     
  4. Store them as Strings but for math there are methods like .multiply() .add() .divide() .minus() etc
     
  5. I would do that but im using more than just those like i am having a scoreboard with their Balance which will most lilkey be bigger than 1billion and things like that so I am needing to go and figure out a way to go and do this efficiently.

    I tried tons of things and tried to google more I have spent about like 10 hours on this project today and im just spent on it and can't really think.

    I tried casting it to long a long and even using new Long(), just hasn't been working for me. Probably not thinking of it any idea?
     
  6. As long as it's not over (about) 2.147 billion, you should be good. And this may help.
     
  7. well with that a long wouldn't be effective cause its gonna be something > 2bil, I am probably gonna go and figure out how to format it to like 100k and things like that.
     
  8. FrostedSnowman

    Resource Staff

    Just use BigInteger.

    Code (Java):
    BigInteger alot = new BigInteger("9182938347671985235272769716");
     
    • Like Like x 1
  9. Will Do,

    I have this (untested) which i think will work

    Code (Java):


    BigInteger balance = new BigInteger("" + Prison.getEconomy().getBalance(Bukkit.getOfflinePlayer(p.getUniqueId())));
    BigInteger rankupPrice = new BigInteger("" + RankupManager.getRankupPrice(p));

     
     
  10. It worked but anything > 2.1 billion which @ReadySetPawn said above, any other ideas?
     
  11. Do you know what "Prison.getEconomy().getBalance()" will return? If it returns a long, a player will never be able to have enough money to afford the rank up price. What does "RankupManager.getRankupPrice()" return? Again, if it returns a long, the rank up price will never be able to be higher than 2.1 billion.

    If those methods can only return a long, it doesn't matter if you cast it to a BigInteger. Those methods will only return < 2.1 billion. So you will just end up with BigIntegers that contain < 2.1 billion.
     
  12. If you want to show the amount in a Human readable way you could just create a method get the size of the number and with that specify if it's million billion etc
     
    • Creative Creative x 1
  13. Yeah that's what i was thinking i was gonna go and do. Just kinda gonna be SUPER tedious. (trying to avoid that) lol
     
  14. Just return a string/enum and do a switch statement
     
  15. getRankupPrice() returns an int.

    getBalance() returns a double, which I will just substring it probably to just the number since i didn't want the decimal if it makes it easier. so both are possible to become > 2.1 billion.
     
  16. still super tedious xD I don't wanna see another switch statement after my tryRankup and getRankupPrice methods..
     
    • Funny Funny x 1
  17. So the BigDecimal worked, since it worked for the Balance which I used BigDecimal for but when i use BigInteger for the large numbers in the config it still doesn't work,

    What could i be doing wrong?

    Code (Java):
     
    p.sendMessage("§eB §7» §F$" + new BigInteger(String.valueOf(Prison.getInstance().ranks.getInt("Ranks.B"))));
     
     
  18. Simpler solution would be to use long data type, but also a valid recommendation.
    What. Any number with the long data type doesn't have to be in a specific range except for between it's MIN and MAX VALUES.
    Here are those:
    Code (Text):
    Long.MAX_VALUE = 9223372036854775807
    Long.MIN_VALUE = -9223372036854775808
    Notice how 2,147,483,647 certainly falls within those ranges. The long specification just means that the number has more memory allocated so that it may be whatever values it needs to be (not a decimal though).
    You're confusing that with Integer.
    Code (Text):
    Integer.MAX_VALUE = 2147483647
    Integer.MIN_VALUE = -2147483648
    So, really, you'd end up with BigIntegers that contain up to 9.2e18.

    getInt returns an int/Integer, so of course that's going to limit you to 2147483647.
    You'll need to either utilize BigInteger and getString() or somehow find a way to serialize BigInteger into the config so that you may cast it to what you get from FileConfiguration#get().

    As a final note, using
    Code (Text):
    unsigned long
    will effectively double the amount you can store as long as you don't need negative balances.

    give me a rating. I worked hard. :<
     
    #18 Drkmaster83, Jan 23, 2018
    Last edited: Jan 23, 2018
    • Agree Agree x 1
    • Winner Winner x 1

  19. I figured it out somehow from this post I red .get() and was like screw it and tried it and it worked. Thank you Solved/
     
Thread Status:
Not open for further replies.