Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọchandling the precision of floating-point numbers in several ways using different functions. Most of them are defined under the “math” module. In this article, we will use high-precision calculations in Python with Decimal in Python.
Bàn luận
Python trong định nghĩa của nó cho phép xử lý độ chính xác của các số điểm nổi theo nhiều cách bằng cách sử dụng các chức năng khác nhau. Hầu hết trong số chúng được xác định theo mô -đun toán học của người Viking. Trong bài viết này, chúng tôi sẽ sử dụng các tính toán có độ chính xác cao trong Python với thập phân trong Python.
- Ví dụ 1: Mã python để chứng minh ceil [], trunc [] và sàn []This function is used to eliminate all decimal parts of the floating-point number and return the integer without the decimal part.
- Trong ví dụ này, chúng tôi sẽ bao gồm một số phương pháp toán học. chẳng hạn như phương thức trunc [], phương thức ceil [] và phương thức sàn [].This function is used to print the least integer greater than the given number.
- Trunc []: Hàm này được sử dụng để loại bỏ tất cả các phần thập phân của số điểm nổi và trả về số nguyên mà không có phần thập phân.This function is used to print the greatest integer smaller than the given integer.
Python3
CLE []: Hàm này được sử dụng để in số nguyên nhỏ nhất lớn hơn số đã cho.
Tầng []: Hàm này được sử dụng để in số nguyên lớn nhất nhỏ hơn số nguyên đã cho.
round[12.54673, 2]
# Returns: 12.55
7 round[12.54673, 2]
# Returns: 12.55
8# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
9# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
3Value: Rounded:
3.14159265359 3.14159
1845.7409947 1845.741
-100.95 -100.95
9.5432 9.54
34.49953 34.5
2# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
5# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
0# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
7# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2Value: Rounded:
3.14159265359 3.14159
1845.7409947 1845.741
-100.95 -100.95
9.5432 9.54
34.49953 34.5
7# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
3round_decimals_up[8.343]
# Returns: 8.35
0# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
5# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
0# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
7# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2round_decimals_up[8.343]
# Returns: 8.35
5Output:
The integral value of number is : 3 The smallest integer greater than number is : 4 The greatest integer smaller than number is : 3
round[12.54673, 2]
# Returns: 12.55
9# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
0 # Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
1
round[12.54673, 2]
# Returns: 12.55
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
3# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
4# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
5# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
0# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
7- Ví dụ 2: Cách giới hạn phao đến hai số thập phân trong Python“%” operator is used to format as well as set precision in python. This is similar to “printf” statement in C programming.
- Trong ví dụ này, chúng ta sẽ thấy cách giới hạn phao đến hai dấu thập phân trong Python. Trong python float chính xác đến 2 phao trong python và python float chính xác đến 3. Có nhiều cách để đặt độ chính xác của các giá trị dấu phẩy động. Một số trong số họ được thảo luận dưới đây. This is yet another way to format the string for setting precision.
- Sử dụng phần trăm%:- toán tử%của%được sử dụng để định dạng cũng như đặt độ chính xác trong Python. Điều này tương tự như tuyên bố của printff trong lập trình C. This
function takes 2 arguments, number, and the number till which we want decimal part rounded.
Python3
Tầng []: Hàm này được sử dụng để in số nguyên lớn nhất nhỏ hơn số nguyên đã cho.
# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
2# Some numerical values
valueA = 3.14159265359
valueB = 1845.7409947
valueC = -100.95
valueD = 9.5432
valueE = 34.49953
# Round to different decimal places
roundA = round[valueA, 5]
roundB = round[valueB, 4]
roundC = round[valueC, 3]
roundD = round[valueD, 2]
roundE = round[valueE, 1]
# Output rounded values
print["Value:".ljust[15], "Rounded:"]
print[str[valueA].ljust[15], roundA]
print[str[valueB].ljust[15], roundB]
print[str[valueC].ljust[15], roundC]
print[str[valueD].ljust[15], roundD]
print[str[valueE].ljust[15], roundE]
3import math
def round_decimals_up[number:float, decimals:int=2]:
"""
Returns a value rounded up to a specific number of decimal places.
"""
if not isinstance[decimals, int]:
raise TypeError["decimal places must be an integer"]
elif decimals