mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
* Changed as suggested Now return in same format as oct() returns * Slight change * Fixed issue #1368, return values for large number now is fixed and does not return in scientific notation * Update decimal_to_octal.py
This commit is contained in:
parent
43905efe29
commit
74d96ab355
@ -6,8 +6,12 @@ import math
|
|||||||
# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js
|
# https://github.com/TheAlgorithms/Javascript/blob/master/Conversions/DecimalToOctal.js
|
||||||
|
|
||||||
|
|
||||||
def decimal_to_octal(num):
|
def decimal_to_octal(num: int) -> str:
|
||||||
"""Convert a Decimal Number to an Octal Number."""
|
"""Convert a Decimal Number to an Octal Number.
|
||||||
|
|
||||||
|
>>> all(decimal_to_octal(i) == oct(i) for i in (0, 2, 8, 64, 65, 216, 255, 256, 512))
|
||||||
|
True
|
||||||
|
"""
|
||||||
octal = 0
|
octal = 0
|
||||||
counter = 0
|
counter = 0
|
||||||
while num > 0:
|
while num > 0:
|
||||||
@ -16,7 +20,7 @@ def decimal_to_octal(num):
|
|||||||
counter += 1
|
counter += 1
|
||||||
num = math.floor(num / 8) # basically /= 8 without remainder if any
|
num = math.floor(num / 8) # basically /= 8 without remainder if any
|
||||||
# This formatting removes trailing '.0' from `octal`.
|
# This formatting removes trailing '.0' from `octal`.
|
||||||
return "{0:g}".format(float(octal))
|
return f"0o{int(octal)}"
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user