mirror of
https://github.com/Redume/Shirino.git
synced 2025-03-14 02:34:36 +03:00
fix: Fixed formatting of numbers. Emphasized zeros at the end
This commit is contained in:
parent
7c3f289bd6
commit
661e11c8d7
1 changed files with 15 additions and 8 deletions
|
@ -2,13 +2,20 @@ from decimal import Decimal
|
||||||
|
|
||||||
def format_number(number):
|
def format_number(number):
|
||||||
number = Decimal(str(number))
|
number = Decimal(str(number))
|
||||||
|
integer_part = number // 1
|
||||||
|
fractional_part = number - integer_part
|
||||||
|
|
||||||
formatted_integer_part = '{:,.0f}'.format(number // 1).replace(',', ' ')
|
formatted_integer = '{:,.0f}'.format(integer_part).replace(',', ' ')
|
||||||
fractional_part = f"{number % 1:.30f}".split('.')[1]
|
|
||||||
|
|
||||||
if int(fractional_part) == 0:
|
if fractional_part == 0:
|
||||||
return formatted_integer_part + ''
|
return formatted_integer
|
||||||
|
|
||||||
significant_start = next((i for i, char in enumerate(fractional_part) if char != '0'), len(fractional_part))
|
fractional_str = f"{fractional_part:.30f}".split('.')[1]
|
||||||
result_fractional = fractional_part[:significant_start + 3]
|
first_non_zero = next((i for i, char in enumerate(fractional_str) if char != '0'), len(fractional_str))
|
||||||
return formatted_integer_part + '.' + result_fractional
|
result_fractional = fractional_str[:first_non_zero + 3]
|
||||||
|
result_fractional = result_fractional.rstrip('0')
|
||||||
|
|
||||||
|
if not result_fractional:
|
||||||
|
return formatted_integer
|
||||||
|
|
||||||
|
return f"{formatted_integer}.{result_fractional}"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue