Two decimal places reserved in Python

Source: Internet
Author: User

I ran into a problem writing a program today about how to control floating-point numbers only show two digits after the decimal point, the normal idea is to use the round function, for exampleround(a, 2),但是在面对下面的问题时候round就不太好用了

>>> a=13.949999999999999
>>> Round (A, 2)
13.949999999999999

On the Internet to check the information, there are netizens to provide a way

Print ('%.2f '%a)

>>>13.95

Alternatively, you can use Trunc (a,2) to intercept the function

>>>13.94

You can also use the decimal
Decimal. The decimal type is a data type that satisfies high-precision calculations in Python and imports the decimal package with import

Define the decimal data type:

1 cannot be defined in the way that the literal value is used

2 are defined in the following ways:

>>> Import decimal >>> x = Decimal. Decimal (87345) >>> x decimal (' 87345 ') >>> x = Decimal. Decimal (' 123.3344332566334 ') >>> x decimal (' 123.3344332566334 ') can be passed to a Decimal integer or string parameter, but cannot be a floating-point data, Because the floating-point data itself is inaccurate

If you need to convert from floating-point data to a decimal type, you might use the following methods

>>> x = Decimal. Decimal.from_float (127.3323)
>>> x
Decimal (' 127.332300000000003592504072003066539764404296875 ')

I had 3 questions pertaining to decimal arithmetic in Python, and all 3 of the which is best asked inline:

1)
FromImport,Decimal>>>().=6>>>Decimal' 50.567898491579878 '  1Decimal' 50.5679 '# How are this a precision of 6? If the decimal counts whole numbersas >>># Part of the precision, are that actually still precision?  >>>     and

2)
FromImport,Decimal>>>().=6>>>Decimal' 50.567898491579878 'Decimal' 50.567898491579878 '# shouldn ' t that has been rounded to 6 digits on instantiation?>>>Decimal' 50.567898491579878 '  1decimal ' 50.5679 ' >>>3)      
# now I want to save the value to my database as a ' total ' with 2 places.>>>FromImportDecimal>>># is the following the correct-to-get the value into 2 decimal places,>>># or is there a "better"?>>>=Decimal' 50.5679 'QuantizeDecimal' 0.00 '# Just wanted to see what's the value wasDecimal' 50.57 '()>>> Native test cases:    
[Python]View Plain copy
  1. >>> Import Decimal
  2. >>> x = Decimal. Decimal (87345)
  3. >>> x
  4. Decimal (' 87345 ')
  5. >>> Print x
  6. 87345
  7. >>> from decimal import getcontext, decimal
  8. >>> x = decimal (' 0.998531571219 '). Quantize (Decimal (' 0.00 '))
  9. >>> x
  10. Decimal (' 1.00 ')
  11. >>> Print x
  12. 1.00
  13. >>> x = decimal (' 0.998531571219 '). Quantize (Decimal (' 0.0000 '))
  14. >>> x
  15. Decimal (' 0.9985 ')
  16. >>> Print x
  17. 0.9985
  18. >>> y = decimal.from_float (0.998531571219)
  19. >>> y
  20. Decimal (' 0.99853157121900004700165709436987526714801788330078125 ')
  21. >>> y = decimal.from_float (0.998531571219). Quantize (Decimal (' 0.0000 '))
  22. >>> y
  23. Decimal (' 0.9985 ')
  24. >>> Print y
  25. 0.9985
  26. >>> f1 = 0.998531571219
  27. >>> F1
  28. 0.998531571219
  29. >>> Type (F1)
  30. <type ' float ' >
  31. >>> F2 = str (f1)
  32. >>> F2
  33. ' 0.998531571219 '
  34. >>> type (F2)
  35. <type ' str ' >
  36. >>>

Two decimal places reserved in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.