I privation a
to beryllium rounded to Thirteen.Ninety five. I tried utilizing round
, however I acquire:
>>> a13.949999999999999>>> round(a, 2)13.949999999999999
For the analogous content with the modular room Decimal people, seat However tin I format a decimal to ever entertainment 2 decimal locations?.
You are moving into the aged job with floating component numbers that not each numbers tin beryllium represented precisely. The bid formation is conscionable displaying you the afloat floating component signifier from representation.
With floating component cooperation, your rounded interpretation is the aforesaid figure. Since computer systems are binary, they shop floating component numbers arsenic an integer and past disagreement it by a powerfulness of 2 truthful Thirteen.Ninety five volition beryllium represented successful a akin manner to 125650429603636838/(2**Fifty three).
Treble precision numbers person Fifty three bits (Sixteen digits) of precision and daily floats person 24 bits (Eight digits) of precision. The floating component kind successful Python makes use of treble precision to shop the values.
For illustration,
>>> 125650429603636838/(2**53)13.949999999999999>>> 234042163/(2**24)13.949999988079071>>> a = 13.946>>> print(a)13.946>>> print("%.2f" % a)13.95>>> round(a,2)13.949999999999999>>> print("%.2f" % round(a, 2))13.95>>> print("{:.2f}".format(a))13.95>>> print("{:.2f}".format(round(a, 2)))13.95>>> print("{:.15f}".format(round(a, 2)))13.949999999999999
If you are last lone 2 decimal locations (to show a foreign money worth, for illustration), past you person a mates of amended selections:
- Usage integers and shop values successful cents, not dollars and past disagreement by A hundred to person to dollars.
- Oregon usage a mounted component figure similar decimal.
Location are fresh format specs, Drawstring Format Specification Mini-Communication:
You tin bash the aforesaid arsenic:
"{:.2f}".format(13.949999999999999)
Line 1: the supra returns a drawstring. Successful command to acquire arsenic interval, merely wrapper with float(...)
:
float("{:.2f}".format(13.949999999999999))
Line 2: wrapping with float()
doesn't alteration thing:
>>> x = 13.949999999999999999>>> x13.95>>> g = float("{:.2f}".format(x))>>> g13.95>>> x == gTrue>>> h = round(x, 2)>>> h13.95>>> x == hTrue
Mistake producing weblog contented
How to change numbers to two decimal places on #microsoftexcel
How to change numbers to two decimal places on #microsoftexcel from Youtube.com