파이썬[Python]: decimal - Decimal 클래스

decimal 모듈 - Decimal 클래스(class)


/// 설명

value 값으로부터 새로운 Decimal 객체를 생성합니다. value값은 정수, 문자열, 튜플, 실수 또는 다른 Decimal 객체일 수 있습니다. 인자가 주어지지 않으면 Decimal('0')을 반환합니다.

※ 형식
class decimal.Decimal(value='0', context=None)

reference
https://docs.python.org/3/library/decimal.html#module-decimal

/// 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from decimal import *
 
print(Decimal())  # 0
print(Decimal(1))  # 1
print(Decimal('1'))  # 1
print(Decimal(0.1))  # 0.1000000000000000055511151231257827021181583404541015625
print(Decimal('0.1'))  # 0.1
print(Decimal((0, (1414), -3)))  # 1.414
print(Decimal('1.414'))  # 1.414
print(Decimal(Decimal(1.1)))  # 1.100000000000000088817841970012523233890533447265625
print(Decimal(Decimal('1.1')))  # 1.1
print(Decimal('\uff11'))  # 1
print(float('1.1'))  # 1.1
print(Decimal(float('1.1')))  # 1.100000000000000088817841970012523233890533447265625
 
# not in calculation ----------
setcontext(BasicContext)  # precision = 9
print(getcontext().prec)  # 9
# the context precision does not affect
print(Decimal('1.1111111111111'))  # 1.1111111111111
 
# in calculation --------------
# the context precision does affect
print(Decimal('1.1111111111111'+ Decimal('1.1111111111111'))  # 2.22222222
 
cs

* 실행환경: Microsoft Windows 10 Homes
* 인터프리터: 파이썬(Python 3.9)


– 당신을 응원합니다. –

댓글

이 블로그의 인기 게시물

파이썬[Python]: 내장함수 - from_bytes 메서드

파이썬[Python]: 내장함수 - __len__ 메서드

파이썬[Python]: kivy - 한글 사용

C 언어: sin 함수, cos 함수, tan 함수

파이썬[Python]: 내장함수 - bit_length 메서드