Files
30-seconds-of-code/snippets/reverse_number.md
Isabelle Viktoria Maciohsek 96a3c7890b Add new snippets
2020-10-04 14:21:41 +03:00

24 lines
537 B
Markdown

---
title: reverse_number
tags: math,string,intermediate
---
Reverses a number.
- Use `str()` to convert the number to a string, slice notation to reverse it and `string.replace()` to remove the sign.
- Use `float()` to convert the result to a number and `copysign()` to copy the original sign.
```py
from math import copysign
def reverse_number(n):
return copysign(float(str(n)[::-1].replace('-','')), n)
```
```py
reverse_number(981) # 189
reverse_number(-500) # -5
reverse_number(73.6) # 6.37
reverse_number(-5.23) # -32.5
```