mirror of
https://github.com/Redume/Shirino.git
synced 2025-02-04 09:58:57 +03:00
feat: add support for grouping separators and precise handling of small numbers in format_number
This commit is contained in:
parent
ef980f1ef6
commit
b5f75ef0bc
1 changed files with 8 additions and 6 deletions
|
@ -3,10 +3,12 @@ from decimal import Decimal
|
||||||
def format_number(number):
|
def format_number(number):
|
||||||
number = Decimal(str(number))
|
number = Decimal(str(number))
|
||||||
|
|
||||||
formatted_integer_part = '{:,.0f}'.format(number).replace(',', ' ')
|
formatted_integer_part = '{:,.0f}'.format(number // 1).replace(',', ' ')
|
||||||
|
fractional_part = f"{number % 1:.30f}".split('.')[1]
|
||||||
|
|
||||||
if '.' in str(number):
|
if int(fractional_part) == 0:
|
||||||
fractional_part = str(number).split('.')[1][:3]
|
return formatted_integer_part + ''
|
||||||
return formatted_integer_part + '.' + fractional_part
|
|
||||||
else:
|
significant_start = next((i for i, char in enumerate(fractional_part) if char != '0'), len(fractional_part))
|
||||||
return formatted_integer_part
|
result_fractional = fractional_part[:significant_start + 3]
|
||||||
|
return formatted_integer_part + '.' + result_fractional
|
||||||
|
|
Loading…
Add table
Reference in a new issue