Java Number Round roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting)

Here you can find the source of roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting)

Description

round Time Value

License

Open Source License

Parameter

Parameter Description
defaultUnitValue a parameter
is24HourFormatting a parameter

Return

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

Declaration

public static float roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting) 

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.//from  ww  w . j a v  a 2s .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
     * @param is24HourFormatting
     * @return Returns minUnitValue rounded to the number 60/30/20/10/5/2/1
     */
    public static float roundTimeValue(final float defaultUnitValue, final boolean is24HourFormatting) {

        float unit = defaultUnitValue;
        int multiplier = 1;

        if (is24HourFormatting) {

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

            if (multiplier >= 3600) {

                unit = //
                        //
                        unit >= 120 ? 120 : //
                                unit >= 60 ? 60 : //
                                        unit >= 30 ? 30 : //
                                                unit >= 15 ? 15 : //
                                                        unit >= 12 ? 12 : //
                                                                unit > 6 ? 6 : //
                                                                        unit > 5 ? 5 : //
                                                                                unit > 2 ? 2 : //
                                                                                        1;

            } else {

                unit = //
                        //
                        unit >= 120 ? 120 : //
                                unit >= 60 ? 60 : //
                                        unit >= 30 ? 30 : //
                                                unit >= 15 ? 15 : //
                                                        unit >= 10 ? 10 : //
                                                                5;
            }

        } else {

            if (unit > 3600) {

                // unit > 1 hour (>3600 sec)

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

                if (multiplier >= 3600) {

                    // multiplier >= 1 hour

                    unit = //
                            //
                            unit > 720 ? 720 : //
                                    unit > 240 ? 240 : //
                                            unit > 120 ? 120 : //
                                                    unit > 24 ? 24 : //
                                                            10;
                    //               unit = //
                    //                     //
                    //               unit > 1000 ? 1000 : //
                    //                     unit > 500 ? 500 : //
                    //                           unit > 100 ? 100 : //
                    //                                 unit > 50 ? 50 : //
                    //                                       10;

                } else {

                    // multiplier < 1 hour (3600 sec)

                    unit = //
                            //
                            unit > 3000 ? 3000 : //
                                    unit > 1200 ? 1200 : //
                                            unit > 600 ? 600 : //
                                                    unit > 300 ? 300 : //
                                                            unit > 120 ? 120 : //
                                                                    60;
                }

            } else {

                // unit < 1 hour (< 3600 sec)

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

                unit = //
                        //
                        unit > 120 ? 120 : //
                                unit > 60 ? 60 : //
                                        unit > 30 ? 30 : //
                                                unit > 15 ? 15 : //
                                                        unit > 10 ? 10 : //
                                                                unit > 5 ? 5 : //
                                                                        unit > 2 ? 2 : //
                                                                                1;
            }
        }

        unit *= multiplier;

        return (long) unit;
    }
}

Related

  1. roundStr(Double value, int decimal)
  2. roundString(double d)
  3. roundString(double n, double range, double precision)
  4. roundTime24h(final long defaultUnitValue)
  5. roundTimeStamp(long tstamp)
  6. roundTo(double aValue, int aDigits)
  7. roundTo(double d, int n)
  8. roundTo(double num, int dp)
  9. roundTo(double val, double prec)