-1

I've looked around and I cannot find an anwser to my question.

I need decimal formatting where the decimal can be different depending on the situation. For this situation I want to pass a variable containing the decimal value.

The values I'm getting from my pandas DataFrame are in this format 3.18e-06, which in this case needs to be turned into 8 decimals, e.g., 3.18123456

Can I either turn my pd DF into an 8 decimal based float64 or can i somehow convert 3.18e-06 into 8 decimals after grabbing it from my db?

Preferably I want to pass a variable containing the decimal for formatting. Something like:

decimal = 0.00000001
{0:.{decimal}f}".format(a)

EDIT:

In the end, none of the suggested options did it for me. Maybe I didn't phrase my question well enough. I'll share my solution here for anyone else who might need it.

ticksize is a variable which changes depending on the Binance Trading pair you're using, it comes in a format like: 0.00001 or 0.0000001.

async def get_precision(ticksize):
    a = '{:.{prec}f}'.format(ticksize, prec=15)
    regex = "..(\d+1).*"
    try:
        b = re.match(regex, str(a))[1]
        precision = len(b)
        return precision
    except Exception as e:
        print(f'An exception has occured on get_precision - {e}')
        return False
# this function returns the length of ticksize starting from the first 0 after the dot. 
# Now that we have our precision we can use a string format to get what we need.

     last_buy = '{:.{prec}f}'.format(a, prec=precision)
     #returns: Last purchase price for XRPBTC: 0.00001588
skurring
  • 9
  • 4
  • for start don't use `decimal` as name - there is `decimal` module in standard library, which may be useful. – buran Jun 22 '22 at 13:17
  • Also https://stackoverflow.com/q/28330657/4046632 – buran Jun 22 '22 at 13:19
  • But all this is relevant if you want to have str. If you want to work with number - look at decimal module and https://stackoverflow.com/q/27638005/4046632 – buran Jun 22 '22 at 13:21
  • And if you work in pandas - check https://stackoverflow.com/q/44963769/4046632 – buran Jun 22 '22 at 13:23
  • Thank you for your reply, I've looked over them and none of them were able to resolve my situation. I may have phrased my question poorly. – skurring Jun 23 '22 at 07:27
  • Actually, your solution is exactly the same what was suggested with regards to precision is string formatting. The extra part is how you "extract" the precision from ticksize number (probably a float?). As suggested in other referenced questions you can easily do the same using the `decimal` module as well (e.g. look at `decimal.Decimal.quantize()`). Still remains the option that you work with pandas dataframe and you can set the precision there as well. – buran Jun 23 '22 at 07:45
  • I'm probably not experienced enough to have gotten it working then. I'm quite new to programming. Thank you for taking the time to send all that information. I may have not gotten it working based on one of them, but the combination of them helped me out a lot. – skurring Jun 24 '22 at 08:32

1 Answers1

0
float("8.99284722486562e-02")  # 0.0899284722486562

and now with 'rounding'

"{:.8f}".format(float("8.99284722486562e-02"))  # '0.08992847'
  • Thanks for your reply, I may have phrased my question poorly, this was not what I needed. I was able to fix it though, I've shared my solution in the original question, – skurring Jun 23 '22 at 07:28