Bạn có thể sử dụng phương thức normalize để loại bỏ độ chính xác thêm. >>> print decimal.Decimal('5.500')
5.500
>>> print decimal.Decimal('5.500').normalize()
5.5
Để tránh tước số không ở bên trái của dấu thập phân, bạn có thể làm điều này: def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
Hoặc nhỏ gọn hơn, sử dụng def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
0 như được đề xuất bởi User7116:def normalize_fraction(d):
normalized = d.normalize()
sign, digit, exponent = normalized.as_tuple()
return normalized if exponent <= 0 else normalized.quantize(1)
Bạn cũng có thể sử dụng def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
1 như được hiển thị ở đây nhưng tôi nghĩ rằng sử dụng def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
2 theo cách này là tự ghi chép hơn.Tôi đã thử nghiệm cả hai so với một vài trường hợp; Vui lòng để lại nhận xét nếu bạn tìm thấy thứ gì đó không hoạt động. >>> normalize_fraction(decimal.Decimal('55.5'))
Decimal('55.5')
>>> normalize_fraction(decimal.Decimal('55.500'))
Decimal('55.5')
>>> normalize_fraction(decimal.Decimal('55500'))
Decimal('55500')
>>> normalize_fraction(decimal.Decimal('555E2'))
Decimal('55500')
Loại bỏ các số không theo dấu vết từ thập phân trong Python #Để loại bỏ các số không kéo dài từ số thập phân: - Sử dụng phương pháp
def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
3 để kiểm tra xem số có phần phân số không. - Nếu có, làm tròn số và trả lại.
- Nếu không, hãy sử dụng phương pháp
def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
4 để tước bất kỳ số không kéo dài nào.
Copied! from decimal import Decimal
num = Decimal('1.230000')
# ✅ drop trailing zeros from decimal, using normalize()
def remove_exponent(d):
return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize()
print(remove_exponent(num)) # 👉️ 1.23
# --------------------------------
# ✅ drop trailing zeros from decimal, using str.rstrip()
num_2 = Decimal('1.230000')
string = str(num_2)
without_trailing_zeros = string.rstrip(
'0').rstrip('.') if '.' in string else string
result = Decimal(without_trailing_zeros)
print(result) # 👉️ 1.23
Hàm đầu tiên được lấy từ phần Câu hỏi thường gặp về các tài liệu chính thức. Phương pháp TO_INTEGRAL làm tròn đến số nguyên gần nhất. Copied! from decimal import Decimal
# 👇️ True
print(Decimal('1.0000') == Decimal('1.0000').to_integral())
# 👇️ False
print(Decimal('1.9000') == Decimal('1.9000').to_integral())
print(Decimal('1.0000').to_integral()) # 👉️ 1
Nếu số không có phần thập phân, chúng tôi sử dụng phương thức def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
5 để làm tròn đến một số vị trí thập phân cố định.Nếu số có phần thập phân, chúng tôi sử dụng phương pháp thập phân. Copied! from decimal import Decimal
print(Decimal('1.230000').normalize()) # 👉️ 1.23
print(Decimal('3.456000000').normalize()) # 👉️ 3.456
Chúng tôi chỉ sử dụng phương thức def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
6 nếu số có phần thập phân vì nó có thể loại bỏ số không ở bên trái của số thập phân.Copied! from decimal import Decimal
print(Decimal('500.000').normalize()) # 👉️ 5E+2
print(Decimal('510.100').normalize()) # 👉️ 510.1
Ngoài ra, bạn có thể sử dụng phương pháp def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
7.Để loại bỏ các số không kéo dài từ số thập phân: - Sử dụng phương pháp
def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
3 để kiểm tra xem số có phần phân số không. - Nếu có, làm tròn số và trả lại.
Copied! from decimal import Decimal
num = Decimal('1.230000')
string = str(num)
without_trailing_zeros = string.rstrip(
'0').rstrip('.') if '.' in string else string
result = Decimal(without_trailing_zeros)
print(result) # 👉️ 1.23
Nếu không, hãy sử dụng phương pháp def normalize_fraction(d):
normalized = d.normalize()
sign, digits, exponent = normalized.as_tuple()
if exponent > 0:
return decimal.Decimal((sign, digits + (0,) * exponent, 0))
else:
return normalized
4 để tước bất kỳ số không kéo dài nào.Hàm đầu tiên được lấy từ phần Câu hỏi thường gặp về các tài liệu chính thức. Đầu tiên chúng tôi dải số không có dấu vết, sau đó cố gắng tước dấu chấm def normalize_fraction(d):
normalized = d.normalize()
sign, digit, exponent = normalized.as_tuple()
return normalized if exponent <= 0 else normalized.quantize(1)
0 nếu phần thập phân chỉ bao gồm các số không theo dõi. Ví dụ cũng kiểm tra xem chuỗi có chứa một dấu chấm hay không, vì vậy chúng tôi không cố gắng tước các số 0 từ các số không có phần thập phân, ví dụ: def normalize_fraction(d):
normalized = d.normalize()
sign, digit, exponent = normalized.as_tuple()
return normalized if exponent <= 0 else normalized.quantize(1)
1 đến def normalize_fraction(d):
normalized = d.normalize()
sign, digit, exponent = normalized.as_tuple()
return normalized if exponent <= 0 else normalized.quantize(1)
2. |