Java Number Round roundTime24h(final long defaultUnitValue)

Here you can find the source of roundTime24h(final long defaultUnitValue)

Description

round Timeh

License

Open Source License

Parameter

Parameter Description
defaultUnitValue a parameter

Return

Returns unit rounded to the number 60/30/20/10/5/2/1

Declaration

public static long roundTime24h(final long defaultUnitValue) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2005, 2016 Wolfgang Schramm and Contributors
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation version 2 of the License./*w  ww .ja v  a  2 s  .co  m*/
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 *******************************************************************************/

public class Main {
    /**
     * @param defaultUnitValue
     * @return Returns unit rounded to the number 60/30/20/10/5/2/1
     */
    public static long roundTime24h(final long defaultUnitValue) {

        float unit = defaultUnitValue;
        int multiplier = 1;

        while (unit > 3600) {
            multiplier *= 3600;
            unit /= 3600;
        }

        float unitRounded = unit;

        if (multiplier >= 3600) {

            // > 1h

            unitRounded =
                    //
                    unitRounded >= 24 //
                            ? 48
                            : unitRounded >= 12 //
                                    ? 24
                                    : unitRounded >= 6 //
                                            ? 12
                                            : unitRounded >= 3 //
                                                    ? 6
                                                    : unitRounded >= 2 //
                                                            ? 2
                                                            : 1;

        } else {

            // <  1h

            unitRounded =
                    //
                    unitRounded >= 1800 ? 1800
                            : unitRounded >= 1200 ? 1200
                                    : unitRounded >= 600 ? 600
                                            : unitRounded >= 240 ? 300
                                                    : unitRounded >= 120 ? 120
                                                            : unitRounded >= 60 ? 60
                                                                    : unitRounded >= 30 ? 30
                                                                            : unitRounded >= 10 ? 20
                                                                                    : unitRounded >= 5 ? 10
                                                                                            : unitRounded >= 2 ? 5
                                                                                                    : unitRounded > 1
                                                                                                            ? 2
                                                                                                            : 1;
        }

        final long unitFinal = (long) (unitRounded * multiplier);

        //      System.out.println(UI.timeStampNano());
        //      System.out.println(UI.timeStampNano() + ("\tdefaultUnitValue\t" + defaultUnitValue));
        //      System.out.println(UI.timeStampNano() + ("\tunit\t\t\t" + unit));
        //      System.out.println(UI.timeStampNano() + ("\tunitRounded\t\t" + unitRounded));
        //      System.out.println(UI.timeStampNano() + ("\tunitFinal\t\t" + unitFinal));
        //      System.out.println(UI.timeStampNano() + ("\tmultiplier\t\t" + multiplier));
        //      // TODO remove SYSTEM.OUT.PRINTLN

        return unitFinal;
    }
}

Related

  1. roundSonekiSuii(double amount1, double amount2)
  2. roundsToTicks(double x)
  3. roundStr(Double value, int decimal)
  4. roundString(double d)
  5. roundString(double n, double range, double precision)
  6. roundTimeStamp(long tstamp)
  7. roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting)
  8. roundTo(double aValue, int aDigits)
  9. roundTo(double d, int n)