Number formatting

Discussion in 'Plugin Development' started by Boobah, May 13, 2017.

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

    Boobah

    I'm trying to get my ban plugin to show with only one decimal place, but not sure how.

    For example, if a player was banned for 2 hours, if they came on 30 minutes later it should say "You are banned for 1.5 Hours" instead of "You are banned for 1.30 Hours"

    My code:
    Code:
    public static String getRemainingTime(String id){
            long current = System.currentTimeMillis();
            long end = getEnd(id);
            if(end == -1){
                return "Permanent";
            }
            long millis =  end - current;
    
            long seconds = 0;
            long minutes = 0;
            long hours = 0;
            long days = 0;
            long weeks = 0;
            while(millis > 1000){
                millis-=1000;
                seconds++;
            }
            while(seconds > 60){
                seconds-=60;
                minutes++;
            }
            while(minutes > 60){
                minutes-=60;
                hours++;
            }
            while(hours > 24){
                hours-=24;
                days++;
            }
            while(days > 7){
                days-=7;
                weeks++;
            }
    
            String r = "";
    
            if(weeks == 0){
                r = days + "." + hours + " Days";
            }
            if(days == 0){
                r = hours + "." + minutes + " Hours";
            }
            if(hours == 0){
                r = minutes + "." + seconds + " Minutes";
            }
            if(minutes == 0){
                r = seconds + " Seconds";
            }
            return r;
    
        }
    Any help?
     
  2. Offline

    Zombie_Striker

    @Boobah
    Just use a Decimal Format:
    Code:
    DecimalFormat numberFormat = new DecimalFormat("#.#");
    System.out.println(numberFormat.format(HOURS));
     
  3. Offline

    Boobah

    But where in my code do I put that?
     
  4. Offline

    Zombie_Striker

    @Boobah
    That bit of code replaces the while loop. All you need to do is convert the milliseconds to hours by dividing the milliseconds by (3,600,000), and returning the string in the println line.
     
Thread Status:
Not open for further replies.

Share This Page