| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

Python Decimal

提供: MyMemoWiki
ナビゲーションに移動 検索に移動

Python Decimal

Python | Python Osmosis

Decimal

  • from decimal で利用
  • 小数点以下の計算も正しく行う
  • getcontext().prec で有効桁数を設定
Python 2.6.3 (r263rc1:75186, Oct  2 2009, 20:40:30) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************
    
>>> from decimal import *
>>> Decimal('0.70') * Decimal('1.05')
Decimal('0.7350')
>>> .70 * 1.05
0.73499999999999999
>>> Decimal('1.00') % Decimal('.10')
Decimal('0.00')
>>> 1.00 % 0.10
0.09999999999999995
>>> sum([Decimal('0.1')] * 10) == Decimal('1.0')
True
>>> sum([0.1] * 10) == 1.0
False
>>> getcontext().prec = 36
>>> Decimal(1) / Decimal(7)
Decimal('0.142857142857142857142857142857142857')