Skip to content

Commit 0734eac

Browse files
committed
fix: return 0x0 from decimal_to_hexadecimal(0)
The loop builds no digit for 0, so the function returned '0x' while its docstring says the results match Python's hex().
1 parent 6e03a48 commit 0734eac

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

‎conversions/decimal_to_hexadecimal.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def decimal_to_hexadecimal(decimal: float) -> str:
2525
"""
2626
take integer decimal value, return hexadecimal representation as str beginning
2727
with 0x
28+
>>> decimal_to_hexadecimal(0)
29+
'0x0'
2830
>>> decimal_to_hexadecimal(5)
2931
'0x5'
3032
>>> decimal_to_hexadecimal(15)
@@ -68,7 +70,7 @@ def decimal_to_hexadecimal(decimal: float) -> str:
6870
while decimal > 0:
6971
decimal, remainder = divmod(decimal, 16)
7072
hexadecimal = values[remainder] + hexadecimal
71-
hexadecimal = "0x" + hexadecimal
73+
hexadecimal = "0x" + (hexadecimal or "0")
7274
if negative:
7375
hexadecimal = "-" + hexadecimal
7476
return hexadecimal

0 commit comments

Comments
 (0)