0

I am implementing my databases & functions for C$50 finance, but am running into some problems in terms of rounding. After certain stocks are bought, or a bad number w/ multiplication, I get remaining cash to be something like 912.700000000001. How would I go about keeping this to 2 decimal places on all parts of my database & program aswell as keep the floating point imprecision to a minimum?

1 Answer 1

1

This answer may help:

pset7, cash on index.html sometimes giving many decimals (eg.4882.139999999999)

And also this answer on how to implement:

https://stackoverflow.com/questions/6036082/call-a-python-function-from-jinja2

For me using the app.jinja statement worked when I put it at the bottom of application.py, and thus the function worked fine in index.html as suggested in the answer.

5
  • That's what I ended up doing. To anyone in the future, make sure to read hints & everything in the spec before doing the PSET.
    – Davis
    Jul 2, 2017 at 22:20
  • 1
    Although now that I am going along (I'm on this pset at the moment too) I think it may be a bad idea to input them with the usd function in buy() because then they can't be manipulated easily later unless I am missing something. For example if I want to buy more shares of the same stock, I then have to change my total price in my portfolio, but now it is classified as a string with a $ in front of it, so I am not sure how to work around that as of yet. Jul 3, 2017 at 0:38
  • Right, you'd want to use it as an output, not perhaps when storing it in the DB.
    – Davis
    Jul 3, 2017 at 17:43
  • @Davis were you able to get it to work? I've been trying to use the usd method when plugging in the price and cost values, but to no success as of yet. Jul 3, 2017 at 21:27
  • @Davis actually I found following what the first answer here seems to work:stackoverflow.com/questions/6036082/… I was sort of confused about the other answers, but I put the app.jinja statement in the very bottom of application.py (subsituting in usd function in) and then the method worked fine to call in in index.htlm. Jul 3, 2017 at 21:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .