1

Very simple, I want to round my 'base_price' and 'entry' with the precision, here 0.00001. It doesn't work because it converts it to a scientific number. How do I do this; I have been stuck for 1 hour.

Some post says to use output="{:.9f}".format(num) but it adds some zero after the last 1.

It work with precision = str(0.0001) but start from 4 zeros after the dot, it change to scientific numbers and digit = precision[::-1].find('.') doesn't work with scientific numbers.

precision = str(0.00001) #5 decimals after the dot
print(precision)
base_price=0.0314858333
entry=0.031525

digit = precision[::-1].find('.')
entry_price = float(round(float(entry), digit))
base_price = float(round(float(base_price), digit))
print(entry_price,base_price)

Expected result:

base_price=0.03148 #5 decimals after the dot
entry=0.03152 #5 decimals after the dot
One boy
  • 216
  • 1
  • 7
  • Why are you going through all that stuff with `str(0.00001)` and `find`? Why not just use `digit=5`? – user2357112 Sep 29 '22 at 20:07
  • 1
    Python uses the hardware's double - a binary float, not a decimal float. The decimal numbers you write are just estimates of the binary number actually used. That's normally okay. But it can play havoc with financial calculations. The `decimal` module does decimal math and lets you set a precision. This is a long winded way of asking you what you want the precision for ... is it just display? Is it needed for your calculations? – tdelaney Sep 29 '22 at 20:16
  • It for precision price, the expected result is base_price=0.03148 entry=0.03152 so 5 decimals after the dot same as the precision price. Needed for calculation – One boy Sep 29 '22 at 20:19
  • 1
    If its just display, `output="{:.5f}".format(entry_price)` should do. – tdelaney Sep 29 '22 at 20:30
  • I have hundreds of it with different price precision – One boy Sep 29 '22 at 20:34
  • 2
    based on some comments you made on some answers, I'd recommend you read this: https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate – user19513069 Sep 29 '22 at 20:40

3 Answers3

1

If you want to round to a precision given by a variable, you can just do

precision = 0.00001
entry = 0.031525
entry_price = round(entry / precision) * precision
LeopardShark
  • 3,820
  • 2
  • 19
  • 33
  • it gives me 0.031525 and 0.0314858333 . Instead of base_price=0.03148 entry=0.03152 – One boy Sep 29 '22 at 20:18
  • @Oneboy what have you done? In my Python 3.10.7, `entry=0.031525;precision = 0.00001;print(round(entry / precision) * precision)` gives `0.03152`. The other is right to floating-point precision as well. – LeopardShark Sep 29 '22 at 20:46
  • try with entry = 0.031526 , it will gives you 0.03153 / I expect 0.03152 – One boy Sep 29 '22 at 21:06
  • 1
    @Oneboy If you want to always round down, replace `round` with `int`. – LeopardShark Sep 29 '22 at 21:16
0

It sounds like you want to truncate the number, so:

precision = 0.00001
base_price = 0.0314858333

# use int to truncate the division to the nearest fraction. 
base_price = int(base_price / precision) * precision 
print(base_price) # prints 0.03148
  • What hardware are you running this on? When I run it on my Dell Latitude 9430 laptop, it shows 0.03148 – Waterski24 Sep 29 '22 at 20:45
  • Can you try with base_price = 0.0314958333 it gives me 0.031490000000000004 – One boy Sep 29 '22 at 21:11
  • This sounds like you're more concerned with how it prints than the accuracy of the calculation, as that 2nd number is extremely close to what you want (0.03149). These numbers are all getting converted to binary floats and back to decimal for display. Binary (base-2) runs into rounding problems just like decimal and can have small errors when converting back and forth. See https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate, as a user commented in the original question. – Waterski24 Sep 29 '22 at 21:20
  • Extremely close is not enough in trading. thanks for the link. I'm reading it – One boy Sep 29 '22 at 21:30
  • Then perhaps using the Decimal library, as another user pointed out. https://docs.python.org/3/library/decimal.html – Waterski24 Sep 30 '22 at 14:07
0

To use numbers in the format '0.00001' to set precision instead of the number you want, I'd use int(abs(math.log10(precision))) then pass that number to format.

Like so

import math
precision = 0.00001
precision_count = int(abs(math.log10(precision)))

base = 0.0314858333
entry = 0.031525

print(f"{entry:.{precision_count}f}, {base:.{precision_count}f}")

(I've removed suffix _price from base_price because in one case you used entry and entry_price and in the other you used base_price twice, so I went with the first standard for both)

I'd recommend you test with multiple possible values of precision

But, you probably should be using Decimal instead

Based on the variable names having "price", you could run into rounding problems/error compounding. To avoid surprises, I recommend you use decimal (or something with similar capabilities). In your case, quantize seems to be what you're looking for.

user19513069
  • 317
  • 1
  • 9