How to use decimal

Get to know decimal numbers

Decimal is based on a floating-point number and often used to represent certain mount of money.


import decimal #  ww w  . j av a 2 s .  c om

print decimal.Decimal('1.1')
print decimal.Decimal(1)
print decimal.Decimal(1.1)

dec = decimal.Decimal('.1')
print dec + decimal.Decimal('1.0')
print dec + decimal.Decimal('1.0')
print decimal.Decimal(1) / decimal.Decimal(7)

Decimal vs float during Arithmetic calculation


from decimal import *       
# w ww  .  j a  v a 2  s. co  m
print Decimal('0.70') * Decimal('1.05')
print .70 * 1.05

print Decimal('1.00') % Decimal('.10')
print 1.00 % 0.10
       
print sum([Decimal('0.1')]*10) == Decimal('1.0')
print sum([0.1]*10) == 1.0

The code above generates the following result.





















Home »
  Python »
    Data Types »




Data Types
String
String Format
Tuple
List
Set
Dictionary