Convert to Float without rounding decimal places

I have a list and it contains a specific number '5.74536541' in it, which I convert to float.

I print it in Python 3 using ("%0.2f" % (variable)) , but it always outputs 5.75 instead of 5.74.

I know what you think, who cares, but this is for the currency conversion program, and I do not want the currencies to be rounded up, but to be precise.

How can I keep it from rounding, but also keep 2 decimal places?

+4
source share
5 answers

You should not use floating point numbers for currencies due to rounding errors, as you mentioned.

It is best to use fixed-precision decimal , where you also have full control over how rounding and trimming work. From the docs:

 >>> from decimal import * >>> getcontext() Context(prec=28, rounding=ROUND_HALF_EVEN, Emin=-999999999, Emax=999999999, capitals=1, flags=[], traps=[Overflow, DivisionByZero, InvalidOperation]) >>> getcontext().prec = 6 >>> Decimal('3.0') Decimal('3.0') >>> Decimal('3.1415926535') Decimal('3.1415926535') >>> Decimal('3.1415926535') + Decimal('2.7182818285') Decimal('5.85987') >>> getcontext().rounding = ROUND_UP >>> Decimal('3.1415926535') + Decimal('2.7182818285') Decimal('5.85988') 

You must represent all currency-based values ​​as Decimals with high precision (the standard level of accuracy must be accurate in your case - just leave prec alone!). If you want to print a beautifully formatted dollar and cents for the user, using the locale module is an easy way to do this.

Be careful when printing, because you will need to quantize decimal to the number of places to display, or rounding will not be based on your decimal context! You should only perform the quantize step for the final display or for one final value - all intermediate steps should use a high-precision decimal to make any operations as accurate as possible.

 >>> from decimal import * >>> import locale >>> locale.setlocale(locale.LC_ALL, '') 'en_AU.UTF-8' >>> getcontext().rounding = ROUND_DOWN >>> TWOPLACES = Decimal(10) ** -2 >>> var = Decimal('5.74536541') Decimal('5.74536541') >>> var.quantize(TWOPLACES) Decimal('5.74') >>> locale.currency(var.quantize(TWOPLACES)) '$5.74' 
+9
source

If you are dealing with currency and accuracy, do not use float , use decimal .

+3
source

Floating point values ​​are known as "useful approximations." No matter what you do for the floating point number, crop it, no matter if the result is a floating point value, you cannot decide how many digits to the right of the decimal point that it has.

Never use floating point values ​​for a currency. See, for example, pydoc decimal . The Python decimal module supports decimal fixed point and decimal floating point arithmetic.

Python docs warn about rounding of floats .

Note. The behavior of round () for floats may be unexpected: for example, round (2.675, 2) gives 2.67 instead of the expected 2.68. This is not a mistake: it is the result of the fact that most decimal fractions cannot be represented exactly as a float.

If you are not careful, you will be misled by the meaning that appears at the prompt of the interpreter .

Python only prints the decimal approximation to the true decimal value of the binary approximation stored by the machine.

and

It is important to realize that this, in the real sense, is an illusion: the value in the machine is not exactly 1/10, you simply round off the display of the true value of the machine. This fact becomes apparent as soon as you try to do arithmetic with these values.

+1
source

Remove the number mod 0.01

i.e.

 rounded = number - (number % 0.01) 

then print it as before.

This suggests that rounding is no more accurate. Are you trying to steal old money from the bank using the rounding error scheme?

+1
source

If the number is a string, then trim the string to two characters after the decimal, and then convert it to float. Otherwise, multiply it by 10 ^ n, where n is the number of digits after the decimal number, and then divide your float by 10 ^ n.

-2
source

All Articles