So that works for that specific example, but it doesn't work if the number changes to 3727.49. It then just rounds it down to 3725 instead of up to 3730.
So that works for that specific example, but it doesn't work if the number changes to 3727.49. It then just rounds it down to 3725 instead of up to 3730.
So you don't want the nearest value ending in 5 which is what I thought you wanted. Since I am not a math expert, who can do this better, this seems to work for both of your requests.
Each has an application that it is best suited for. None of them are really interchangeable.
from your example, 3727.49 should round down to 3725.
you can try roundUp to get your preferred answer of 3730, but asking for round to get you that answer will not work.
Your example, rounding 27.49 up to 30 by thinking that the .49 rounds up to .5 and then .5 rounds up again to 30 is called double rounding, and is not generally employed by algorithms. The algorithm looks at the 27.4 and decides that .4 rounds down, and so 27.49 rounds down to 27 rather than up to 28 and therefore your request to get to the nearest 5 is based on 27 (which goes down) rather than 28 (which would go up).
This is probably a lot longer an answer than you wanted, but I spend a lot of time thinking about how different programs and databases work with rounding. And don't ask me to explain MS Excel does it. I don't want to think about it!!! ;-)
Thanks @mashume for the detailed explanation. It got me thinking a bit more and I was able to figure out a workaround in my script using your suggestions and @SleepyZ's original solution
So that works for that specific example, but it doesn't work if the number changes to 3727.49. It then just rounds it down to 3725 instead of up to 3730.
useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.
How do I get started?
We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.
If you are new, or just looking for guidance, here are some helpful links to get you started.
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.