Float3 Python _ Decimal 모듈로 부동 소수점 문제 해결하기 Decimal 이란? Decimal은 '부동 소수점 문제'를 해결하고 소수점을 정확하게 표현하기 위해 사용하는 Python 자료형이다. 컴퓨터에서는 소수를 이진수로 표현하다 보니, 0.1과 같은 값이 정확하게 표현되지 않을 수 있다. 이러한 문제 때문에 금융, 회계 등 정확한 계산이 필요한 경우에는 Decimal을 사용해야 한다. 예를 들어, 파이썬에서 소수형을 그대로 사용하면 다음과 같은 부동 소수점 문제가 발생할 수 있다. value = 0.1 + 0.2 print(value) # 출력: 0.30000000000000004 하지만 아래처럼 Decimal 모듈을 사용하면 이런 문제를 피할 수 있다. from decimal import Decimal value = Decimal('0.1') + Decim.. 2023. 5. 16. 1222. Formatting decimal places #0 Each number should be formatted that it is rounded to two decimal places. You don't need to check whether the input is a valid number because only valid numbers are used in the tests. Example: 5.5589 is rounded 5.56 3.3424 is rounded 3.34 Solution: def two_decimal_places(n: float) -> float: l, r = str(n).split(".") r = str(round(int("1"+r[:5]), -3)) return float(".".join([l, r[1:3]])) def two_de.. 2022. 12. 23. GROCERY STORE: Real Price! You are the owner of the Grocery Store. All your products are in the database, that you have created after CodeWars SQL exercises! :) Customer often need to now how much really they pay for the products. Manufacturers make different sizes of same product so it is hard to compare prices, sometimes they make packages look big, but the weight of the product is small. Make a SELECT query which will .. 2022. 7. 8. 이전 1 다음