Java Fraction Format formatRate(double events, double time, String event)

Here you can find the source of formatRate(double events, double time, String event)

Description

Renders a human readable event rate esitmation.

License

Open Source License

Parameter

Parameter Description
events number of events.
time timespan in seconds.
event event name.

Return

a human readable event rate esitmation.

Declaration

public static String formatRate(double events, double time, String event) 

Method Source Code

//package com.java2s;

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Main {
    /**// www  .  j  a  v  a 2 s .c  o m
     * Renders a human readable event rate esitmation.
     * 
     * @param events number of events.
     * @param time timespan in seconds.
     * @param event event name.
     * @return a human readable event rate esitmation.
     */
    public static String formatRate(double events, double time, String event) {
        StringBuffer buff = new StringBuffer();
        NumberFormat format = new DecimalFormat("#.##");
        if (events > time) {
            buff.append(format.format(events / time) + " " + event + "s / 1s on average");
        } else {
            double interval = time / events;
            int d = (int) (interval / (24 * 3600));
            interval -= d * 24 * 3600;
            int h = (int) (interval / 3600);
            interval -= h * 3600;
            int m = (int) (interval / 60);
            interval -= m * 60;
            buff.append("1 " + event + " / ");
            if (d > 0) {
                buff.append(d + "d ");
            }
            if (h > 0 || d > 0) {
                buff.append(h + "h ");
            }
            if (m > 0 || h > 0 || d > 0) {
                buff.append(m + "m ");
            }
            buff.append(format.format(interval) + " s on average");
        }
        return buff.toString();
    }
}

Related

  1. formatPair(double in, double out)
  2. formatPotencia(float potencia)
  3. formatPowerFloat(float averageRfTickSent)
  4. formatPrice(Double price)
  5. formatQuantity(Double quantity)
  6. formatSimpleDecimal(double d)
  7. formatSpeed(float speed)
  8. formatted_string(double number)
  9. formatToEightPlaces(double pValue)