mirror of
https://github.com/Redume/Shirino.git
synced 2025-03-13 18:24:37 +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):
|
||||
number = Decimal(str(number))
|
||||
integer_part = number // 1
|
||||
fractional_part = number - integer_part
|
||||
|
||||
formatted_integer_part = '{:,.0f}'.format(number // 1).replace(',', ' ')
|
||||
fractional_part = f"{number % 1:.30f}".split('.')[1]
|
||||
formatted_integer = '{:,.0f}'.format(integer_part).replace(',', ' ')
|
||||
|
||||
if int(fractional_part) == 0:
|
||||
return formatted_integer_part + ''
|
||||
|
||||
significant_start = next((i for i, char in enumerate(fractional_part) if char != '0'), len(fractional_part))
|
||||
result_fractional = fractional_part[:significant_start + 3]
|
||||
return formatted_integer_part + '.' + result_fractional
|
||||
if fractional_part == 0:
|
||||
return formatted_integer
|
||||
|
||||
fractional_str = f"{fractional_part:.30f}".split('.')[1]
|
||||
first_non_zero = next((i for i, char in enumerate(fractional_str) if char != '0'), len(fractional_str))
|
||||
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