Skip to content

Commit 74d96ab

Browse files
LawJarp-Acclauss
authored andcommitted
* Changed as suggested Now return in same format as oct() returns * Slight change * Fixed issue TheAlgorithms#1368, return values for large number now is fixed and does not return in scientific notation * Update decimal_to_octal.py
1 parent 43905ef commit 74d96ab

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

conversions/decimal_to_octal.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js
77

88

9-
def decimal_to_octal(num):
10-
"""Convert a Decimal Number to an Octal Number."""
9+
def decimal_to_octal(num: int) -> str:
10+
"""Convert a Decimal Number to an Octal Number.
11+
12+
>>> all(decimal_to_octal(i) == oct(i) for i in (0, 2, 8, 64, 65, 216, 255, 256, 512))
13+
True
14+
"""
1115
octal = 0
1216
counter = 0
1317
while num > 0:
@@ -16,7 +20,7 @@ def decimal_to_octal(num):
1620
counter += 1
1721
num = math.floor(num / 8) # basically /= 8 without remainder if any
1822
# This formatting removes trailing '.0' from `octal`.
19-
return "{0:g}".format(float(octal))
23+
return f"0o{int(octal)}"
2024

2125

2226
def main():

0 commit comments

Comments
 (0)