Hướng dẫn how do you convert a float to 2 decimal places in python? - làm thế nào để bạn chuyển đổi một số float thành 2 chữ số thập phân trong python?

Tldr;]

Vấn đề làm tròn của đầu vào và đầu ra đã được giải quyết dứt khoát bằng Python 3.1 và bản sửa lỗi cũng được đưa vào Python 2.7.0.solved definitively by Python 3.1 and the fix is backported also to Python 2.7.0.

Các số tròn có thể được chuyển đổi đảo ngược giữa phao và chuỗi qua lại:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
0 hoặc
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
1
back and forth:
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
0 or
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
1

>>> 0.3
0.3
>>> float[repr[0.3]] == 0.3
True

Một loại

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
2 không cần thiết để lưu trữ nữa.

Kết quả của các hoạt động số học phải được làm tròn một lần nữa vì các lỗi làm tròn có thể tích lũy không chính xác hơn so với điều đó có thể xảy ra sau khi phân tích một số. Không được cố định bởi thuật toán

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
3 được cải thiện [Python> = 3.1,> = 2.7.0]: because rounding errors could accumulate more inaccuracy than that is possible after parsing one number. That is not fixed by the improved
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
3 algorithm [Python >= 3.1, >= 2.7.0]:

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]

The output string function

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
4 was rounded to 12 valid digits in Python < 2.7x and < 3.1, to prevent excessive invalid digits similar to unfixed repr[] output. That was still insufficientl after subtraction of very similar numbers and it was too much rounded after other operations. Python 2.7 and 3.1 use the same length of str[] although the repr[] is fixed. Some old versions of Numpy had also excessive invalid digits, even with fixed Python. The current Numpy is fixed. Python versions >= 3.2 have the same results of str[] and repr[] function and also output of similar functions in Numpy.

Bài kiểm tra

import random
from decimal import Decimal
for _ in range[1000000]:
    x = random.random[]
    assert x == float[repr[x]] == float[Decimal[repr[x]]]  # Reversible repr[]
    assert str[x] == repr[x]
    assert len[repr[round[x, 12]]] >> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
5,
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
6 và
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
7; phân tích cú pháp phao và văn học tưởng tượng trong mã Python; và chuyển đổi thập phân sang phao. between floating-point numbers and strings are now correctly rounded on most platforms. These conversions occur in many different places: str[] on floats and complex numbers; the float and complex constructors; numeric formatting; serializing and de-serializing floats and complex numbers using the
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
5,
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
6 and
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
7 modules; parsing of float and imaginary literals in Python code; and Decimal-to-float conversion.

Liên quan đến điều này, phần repr [] của số điểm nổi x hiện đang trả về kết quả dựa trên chuỗi thập phân ngắn nhất mà Lọ đảm bảo quay trở lại X dưới cách làm tròn chính xác [với chế độ làm tròn nửa vòng tròn]. Trước đây, nó đã đưa ra một chuỗi dựa trên làm tròn x đến 17 chữ số thập phân.repr[] of a floating-point number x now returns a result based on the shortest decimal string that’s guaranteed to round back to x under correct rounding [with round-half-to-even rounding mode]. Previously it gave a string based on rounding x to 17 decimal digits.

Vấn đề liên quan

Thông tin thêm: Định dạng

>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
8 trước Python 2.7 tương tự như
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
9 hiện tại. Cả hai loại đều sử dụng cùng độ chính xác gấp đôi của IEEE 754 với 52 bit mantissa. Một sự khác biệt lớn là
import random
from decimal import Decimal
for _ in range[1000000]:
    x = random.random[]
    assert x == float[repr[x]] == float[Decimal[repr[x]]]  # Reversible repr[]
    assert str[x] == repr[x]
    assert len[repr[round[x, 12]]]  0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
8 before Python 2.7 was similar to the current
>>> 0.1 + 0.2
0.30000000000000004
>>> 0.1, 0.2, 0.3
[0.1, 0.2, 0.3]
9. Both types use the same 64 bit IEEE 754 double precision with 52 bit mantissa. A big difference is that
import random
from decimal import Decimal
for _ in range[1000000]:
    x = random.random[]
    assert x == float[repr[x]] == float[Decimal[repr[x]]]  # Reversible repr[]
    assert str[x] == repr[x]
    assert len[repr[round[x, 12]]] 

Bài Viết Liên Quan

Chủ Đề