Skip to content
This repository was archived by the owner on May 7, 2023. It is now read-only.

Files

Latest commit

 

History

History
26 lines (21 loc) · 597 Bytes

reverse-number.md

File metadata and controls

26 lines (21 loc) · 597 Bytes
title type tags cover dateModified
Reverse number
snippet
math
taking-photos
2020-11-02 19:28:27 +0200

Reverses a number.

  • Use str() to convert the number to a string, slice notation to reverse it and str.replace() to remove the sign.
  • Use float() to convert the result to a number and math.copysign() to copy the original sign.
from math import copysign

def reverse_number(n):
  return copysign(float(str(n)[::-1].replace('-', '')), n)
reverse_number(981) # 189
reverse_number(-500) # -5
reverse_number(73.6) # 6.37
reverse_number(-5.23) # -32.5