DEV Community

Link Jen
Link Jen

Posted on

How to convert decimal hours to hours and minutes

Payroll systems and time trackers often store a workday as a decimal. Seven hours and forty-five minutes becomes 7.75. That's convenient for arithmetic: 7.75 plus 1.25 is exactly 9.0. It's less convenient when you have to print the same value as 7:45 on a timesheet.

The conversion is short. The bugs I've run into are almost always in the same places: how you round a fractional minute, what you do with a negative adjustment, and whether a total over 24 hours should wrap like a clock or stay a duration.

The formula

Treat the decimal as a duration, not a time of day.

  1. Multiply the decimal hours by 60 to get a total number of minutes.
  2. Round that total to an integer, using an explicit rounding rule.
  3. Split the integer with division and remainder: hours are the quotient, minutes are the remainder.

So 7.75 × 60 = 465 minutes, and 465 ÷ 60 is 7 hours with 45 left over. Result: 7:45.

The same steps work for a weekly total. 25.5 × 60 = 1,530 minutes, which is 25 hours and 30 minutes, not 1:30 the next morning.

If you only need to convert a single value and don't want to open a spreadsheet, Decimal to Time will do that in a browser. The snippets below are for when the conversion has to live in your own code or timesheets.

Rounding

Almost every off-by-one I've seen in this conversion comes from rounding. 1.995 hours is 1 hour plus 0.995 × 60 = 59.7 minutes. If you take the integer part of the hours first and then round the leftover minutes, 59.7 becomes 60, and you have to carry that extra hour. Forget the carry and you print 1:60, which isn't a valid time.

It's more reliable to round the total minutes first, then split:

  • 1.995 × 60 = 119.7
  • Round to 120
  • 120 ÷ 60 = 2 hours, remainder 0
  • Result: 2:00

You also need to pick a rounding mode and stick to it. The snippets below use half away from zero: 0.5 minutes rounds to 1, and -0.5 minutes rounds to -1. That's usually what payroll and timesheets expect.

Don't assume a language's built-in round already does that.

Python 3's round() uses banker's rounding (ties go to the even integer), so round(2.5) is 2 and round(3.5) is 4. The Python snippet uses Decimal.ROUND_HALF_UP instead, which is half away from zero.

JavaScript's Math.round rounds ties toward +Infinity, so Math.round(1.5) is 2 but Math.round(-1.5) is -1. The snippet rounds the absolute value first, then restores the sign, so ties behave the same in both directions.

Floating-point representation can also nudge a value that should be exact, especially after you sum many entries. In Python, Decimal(str(value)) keeps ordinary timesheet values in base 10. In JavaScript, rounding the total minutes is usually enough. If you need exact decimal arithmetic, use a decimal library, or store minutes as integers from the start.

Negative values

Negative decimal hours show up as adjustments: a correction of -1.25 hours, a balance of -0.5 hours, or a reversal of a previous entry. Dropping the sign, or taking a clock-style modulo of a negative number, will give you the wrong duration.

A convention that works in practice:

  • For one hour or more, keep minutes in the range 0 to 59 and put the sign on the hours. -7.75 becomes -7:45, not -8:15 and not 7:-45.
  • Between 0 and -1 hour, there are no hours to hold the sign. -0.5 hours is 0 hours and -30 minutes, which you should still format as -0:30.

If you only attach the sign to the hours component, sign * 0 is just 0, and the minus sign disappears. Both snippets below treat that case explicitly.

If a negative duration isn't valid in your system, reject it with an error. Don't silently take the absolute value.

More than 24 hours

7.75 is a duration. So is 25.5, and so is 100.25 (100 hours and 15 minutes). These values appear in weekly timesheets, project totals, and overtime reports.

Don't reduce the hours modulo 24 unless you are converting a duration into a clock time on a 24-hour face, which is a different problem. 25:30 as a duration and 01:30 as a time of day mean different things. Excel's default h:mm format wraps at 24 hours; you need [h]:mm to keep the full hour count. The code snippets do not wrap.

JavaScript

These functions convert a finite number of decimal hours to { hours, minutes } and format them as H:MM. I ran the examples with Node.js.

function decimalHoursToHm(value) {
  if (typeof value !== "number" || !Number.isFinite(value)) {
    throw new TypeError("value must be a finite number");
  }
  // Math.round is half toward +Infinity, so round the absolute
  // value and restore the sign (half away from zero).
  const sign = value < 0 ? -1 : 1;
  const totalMinutes = sign * Math.round(Math.abs(value) * 60);
  const absolute = Math.abs(totalMinutes);
  const hours = Math.floor(absolute / 60);
  const minutes = absolute % 60;
  if (sign < 0) {
    if (hours === 0) {
      return { hours: 0, minutes: -minutes };
    }
    return { hours: -hours, minutes };
  }
  return { hours, minutes };
}

function formatHm({ hours, minutes }) {
  const sign = hours < 0 || minutes < 0 ? "-" : "";
  return `${sign}${Math.abs(hours)}:${String(Math.abs(minutes)).padStart(2, "0")}`;
}
Enter fullscreen mode Exit fullscreen mode

Examples:

formatHm(decimalHoursToHm(7.75));    // "7:45"
formatHm(decimalHoursToHm(1.995));   // "2:00"
formatHm(decimalHoursToHm(25.5));    // "25:30"
formatHm(decimalHoursToHm(-7.75));   // "-7:45"
formatHm(decimalHoursToHm(-0.5));    // "-0:30"
Enter fullscreen mode Exit fullscreen mode

Python

The Python version does the same conversion with decimal.Decimal, so the arithmetic stays in base 10.

from decimal import Decimal, ROUND_HALF_UP


def decimal_hours_to_hm(value) -> tuple[int, int]:
    hours_decimal = Decimal(str(value))
    total_minutes = int(
        (hours_decimal * 60).to_integral_value(rounding=ROUND_HALF_UP)
    )
    sign = -1 if total_minutes < 0 else 1
    hours, minutes = divmod(abs(total_minutes), 60)
    if sign < 0:
        if hours == 0:
            return 0, -minutes
        return -hours, minutes
    return hours, minutes


def format_hm(hours: int, minutes: int) -> str:
    sign = "-" if hours < 0 or minutes < 0 else ""
    return f"{sign}{abs(hours)}:{abs(minutes):02d}"
Enter fullscreen mode Exit fullscreen mode

Decimal(str(value)) is deliberate. Decimal(7.75) would first go through a binary float; Decimal("7.75") or Decimal(str(7.75)) keeps the decimal you actually wrote. The examples above produce the same strings as the JavaScript version.

Excel and Google Sheets

If the decimal hours are in cell A1, this formula rounds to the nearest minute and formats the result as a duration, including values over 24 hours and negative values:

=IF(A1<0,"-","")&TEXT(ROUND(ABS(A1)*60,0)/1440,"[h]:mm")
Enter fullscreen mode Exit fullscreen mode

ABS(A1)*60 is the total minutes; ROUND(...,0) is the same nearest-minute step as in the code. Dividing by 1440 (minutes in a day) makes an Excel or Sheets time value. [h]:mm prints hours as a duration. Plain h:mm would wrap 25.5 into 1:30. The leading IF prepends a minus sign, because TEXT on a negative time is unreliable (Excel often shows #####).

If you'd rather not use TEXT, you can build the string from integer hours and minutes. This works in current Excel (LET) and in Google Sheets:

=LET(m,ROUND(A1*60,0),IF(m<0,"-","")&INT(ABS(m)/60)&":"&TEXT(MOD(ABS(m),60),"00"))
Enter fullscreen mode Exit fullscreen mode

In older Excel without LET, put =ROUND(A1*60,0) in a helper cell and use the same IF / INT / MOD pattern on that cell.

Avoid =TEXT(A1/24,"h:mm") for timesheets. It skips an explicit minute-rounding step and wraps at 24 hours, so 25.5 becomes 1:30, and 1.995 can display as 1:59 or 2:00 depending on the remainder.

Checklist

  • Convert total minutes first, then split into hours and minutes, so a rounded 60 minutes becomes an extra hour.
  • State the rounding mode. Half away from zero is a reasonable default for payroll; don't assume round() or Math.round already does that.
  • Treat the result as a duration. Only wrap at 24 hours if you are actually formatting a clock time.
  • Preserve the sign, including for values between -1 and 0 hours.
  • In spreadsheets, use [h]:mm, not h:mm.

Top comments (0)