Rounding, and putting Million, or Billion

Discussion in 'Plugin Development' started by Niknea, Apr 2, 2014.

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

    Niknea

    Hey guys, I am creating a scoreboard and I would like to add this feature but don't know how. On the scoreboard it shows the players money, I would like it to round the money to to make it a whole number, and add a "M" at the end if the value is in the millions, or a "B" if it's in the billions. How can I go along doing this? Also, if its in the millions, I would like only the first 3 numbers to show (last number rounded down no matter what). For billions same thing, except for numbers show.

    Any help would be great, thanks!
     
  2. Offline

    rfsantos1996

    Use values of scoreboards' "OfflinePlayers" as lines.

    Scoreboard:
    Money: 2
    10M 1

    I don't think you can round scoreboards' value, you need to do that as a normal String, thats easier I suppose:

    Code:java
    1. if money > 1 bi {
    2. return new DecimalFormat("0.0B").format((money*1.0D / 1,000,000,000.0D));
    3. } else if money > 1 mi {
    4. return new DecimalFormat("0.0M").format((money*1.0D / 1,000,000.0D));
    5. // don't include "," i made it so I can see if I wrote the right number
    6. } else if money > 1 k {
    7. return new DecimalFormat("0.0k").format((money*1.0D / 1000.0D));
    8. } else {
    9. return money;


    1125562,36 / 1000000 = 1,12556236 -> 1.1M (if you add another "0" after "o.o", it'll show another house -> 1.12M)

    I got this method from my K/D calculation, that only shows 2 numbers after ,
    Code:java
    1. public String getKillDeathRatio() {
    2. int death = deaths;
    3. if(death < 1) {
    4. death = 1;
    5. }
    6. return new DecimalFormat("0.00").format((kills * 1D / death * 1D));
    7. // integers = need to be *1.0D, so the result can be given in double and not rounded int values
    8. }
     
  3. Offline

    AoH_Ruthless

    Niknea
    There could be a better way (didn't put too much thought), but here we go (for millions):

    Code:java
    1. // Define the number
    2. int i = (int) Math.round(/*value*/);
    3. String str = String.valueOf(i);
    4.  
    5. // Check if it's in the millions
    6. if (str.length() >= 7 && str.length() <= 9) {
    7. // Substring it, starting with the first number to the third number (2), then add the M tag.
    8. str = str.substring(0, 2) + " M";
    9. // Add the value to your scoreboard (assuming you know how to do it)
    10. }


    Now do the same for billions...
    Hoped it helped!
     
  4. Offline

    Niknea

  5. Offline

    AoH_Ruthless

    Niknea
    Don't take this the wrong way but..

    Use that noggin of yours; think about it: What would have to change to check billions instead of millions?? (I'm trying to get you to try and solve your own problem)
     
  6. Offline

    rfsantos1996

    Math isnt exacly required to coding but please, AoH_Ruthless got a point
     
    AoH_Ruthless likes this.
  7. Offline

    Niknea

    AoH_Ruthless Ahh my bad, got it, also, do you know a good guide for math.round(), not to familiar with it.
     
  8. Offline

    AoH_Ruthless

    Niknea
    it rounds a number and returns a long (that's why I casted to int). Math.floor() truncates numbers.
     
Thread Status:
Not open for further replies.

Share This Page