Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:43:50 +03:00
parent ab1ea476c5
commit a5ca5190e5
169 changed files with 0 additions and 626 deletions

View File

@ -0,0 +1,26 @@
---
title: Reverse number
type: snippet
tags: [math]
cover: taking-photos
dateModified: 2020-11-02T19:28:27+02:00
---
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.
```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
```