Problems in Decimal Package

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nk28
    New Member
    • Jan 2010
    • 26

    Problems in Decimal Package

    I am having a very strange problem which I am unable to figure out.

    I wanted all my data to be printed in decimals and not in exponential terms.

    So I am using decimal module.

    print "\n GENUINE ACCEPTANCE RATE :\t ", GA/Decimal(users)

    Now I have set the precision to 9 and when I try to run in the shell it gives me the correct value but during execution of this program for GA=2 and users = 180,000,000 it still provides me exp answer.

    And that too it gives me only for this value . Can anybody explain why this is happening.....

    Thanks a lot...
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    2/180 million probably has too many leading zeros to fit within the 9 precision limitation, or it is the series of infinitely repeating ones that is larger than 9 precision. Try 2/180,000 as an experiment to see if that works. Then check the .quantize member of the decimal class.
    Code:
    from decimal import *
    getcontext()
    Context(prec=9, rounding=ROUND_HALF_UP, 
            capitals=1, flags=[], traps=[Overflow, DivisionByZero,
            InvalidOperation])
    x = Decimal(2)
    y = Decimal(180000)
    result1 = x/y
    print result1
    
    print Decimal(result1).quantize(Decimal('.00000001'))

    Comment

    • nk28
      New Member
      • Jan 2010
      • 26

      #3
      Hey,

      It seems you are right as now both the values that you gave print 1.11E-8 , even when I have set the precision to 28.

      IS there any way I can get this to work in decimal way.

      Thanks a lot....

      Comment

      Working...