2025-01-09 00:18:24 +03:00
|
|
|
from decimal import Decimal
|
2025-01-08 22:58:12 +03:00
|
|
|
|
2025-01-09 00:18:24 +03:00
|
|
|
def format_number(number):
|
|
|
|
number = Decimal(str(number))
|
2025-02-04 14:39:02 +03:00
|
|
|
integer_part = number // 1
|
|
|
|
fractional_part = number - integer_part
|
2025-01-09 23:03:38 +03:00
|
|
|
|
2025-02-04 14:39:02 +03:00
|
|
|
formatted_integer = '{:,.0f}'.format(integer_part).replace(',', ' ')
|
2025-01-09 23:03:38 +03:00
|
|
|
|
2025-02-04 14:39:02 +03:00
|
|
|
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}"
|