Skip to content

Reverse a number including decimal points in Python


from math import copysign

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

Source: 30 Seconds of Python

See also  How to check if a list is empty in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.