小数格式小时.百分制与hh:mm tt时间格式双向转换需求
Alright, let's solve this two-way time conversion problem—turning that decimal hour.hundredths format into hh:mm tt (12-hour with AM/PM), and vice versa. Let's start by breaking down the logic behind each conversion, then share working code examples (using Python, since it's straightforward for this kind of task).
Decimal to hh:mm tt Conversion
First, let's unpack the pattern from your examples: the decimal's fractional part represents a percentage of an hour, not actual minutes. So 0.01 in the decimal equals 0.6 real minutes (since 60 minutes divided by 100 = 0.6). Here's the step-by-step logic:
- Split the decimal into its integer hour part (
raw_hour) and fractional part. For example,23.08→raw_hour=23,fraction=0.08. - Calculate minutes by multiplying the fraction by 60, then rounding to the nearest integer. For
0.08*60=4.8, that rounds to 5 minutes. - Convert the 24-hour
raw_hourto 12-hour format and set AM/PM:0→ 12 AM- 1–11 → keep the hour, use AM
- 12 → 12 PM
- 13–23 → subtract 12 from the hour, use PM
- Pad hours and minutes with leading zeros if needed to make them two digits.
Python Code for Decimal → hh:mm tt
def decimal_to_hhmmtt(decimal_time): raw_hour = int(decimal_time) fraction = decimal_time - raw_hour minutes = round(fraction * 60) # Handle edge case where rounding pushes minutes to 60 (e.g., 11.995 → rounds to 60) if minutes == 60: raw_hour += 1 minutes = 0 # Convert to 12-hour format and set period if raw_hour == 0: hour_12 = 12 period = "AM" elif 1 <= raw_hour <= 11: hour_12 = raw_hour period = "AM" elif raw_hour == 12: hour_12 = 12 period = "PM" else: # 13-23 hour_12 = raw_hour - 12 period = "PM" # Format with leading zeros for two-digit hours/minutes return f"{hour_12:02d}:{minutes:02d} {period}" # Test your examples print(decimal_to_hhmmtt(11.00)) # 11:00 AM print(decimal_to_hhmmtt(11.02)) # 11:01 AM print(decimal_to_hhmmtt(11.92)) # 11:55 AM print(decimal_to_hhmmtt(23.00)) # 11:00 PM print(decimal_to_hhmmtt(23.08)) # 11:05 PM
hh:mm tt to Decimal Conversion
For the reverse, we need to turn a 12-hour time back into that hour.hundredths decimal. The key here is reversing the earlier math:
- Split the input string into the time part and AM/PM period (e.g.,
"11:01 PM"→ time part"11:01", period"PM"). - Convert the 12-hour hour to 24-hour format (
raw_hour):- AM: if hour is 12 → raw_hour=0; else keep the hour as-is
- PM: if hour is 12 → raw_hour=12; else add 12 to the hour
- Calculate total minutes in 24-hour time:
raw_hour * 60 + minutes. - Convert total minutes to decimal hours:
total_minutes / 60, then round to two decimal places to match the original format.
Python Code for hh:mm tt → Decimal
def hhmmtt_to_decimal(time_str): # Split time string into time component and period time_part, period = time_str.split() hour_12, minutes = map(int, time_part.split(':')) # Convert to 24-hour format if period.upper() == "AM": raw_hour = 0 if hour_12 == 12 else hour_12 else: # PM raw_hour = 12 if hour_12 == 12 else hour_12 + 12 # Calculate decimal time, rounded to two decimal places total_minutes = raw_hour * 60 + minutes decimal_time = total_minutes / 60 return round(decimal_time, 2) # Test your examples print(hhmmtt_to_decimal("11:00 PM")) # 23.00 print(hhmmtt_to_decimal("11:01 PM")) # 23.02 print(hhmmtt_to_decimal("11:35 AM")) # 11.58 print(hhmmtt_to_decimal("11:00 AM")) # 11.00
Quick Notes
- Edge cases: If rounding pushes minutes to 60 (e.g.,
11.995), the code adjusts the hour up by 1 and sets minutes to 0 (so11.995becomes12:00 PM). - Input validation: If you need to handle malformed inputs (like invalid time strings or decimals outside 0.00–23.99), you can add checks to the functions (e.g., using regex for time strings, or clamping decimal values).
内容的提问来源于stack exchange,提问作者user3038537

