SimpleCryptoLife

[SCL] Significant Figures Example Function

This script consist of a single example function that takes a floating-point number - one that can, but doesn't have to, include a decimal point - and converts it to a floating-point number with only a certain number of significant digits left.

I'm not aware of another script that does this. There might well be a simpler way, in which case please do let me know.

For example, say you want to display a variable from your script to the user and it comes out to something like 45.366666666666666666666667 or whatever. That looks awful when you, for example, print it in a label.
Now, you could round it up to the nearest integer easily using a built-in function, or even to a certain number of decimal places using a reasonably simple custom function.
But that's a bit arbitrary. Suppose you don't know what asset the script will be used on, and so you can't predict what the price is, and what the value will turn out to be.
It could be 0.00045366666666666666666666667 instead. Now if you round it up to 3 decimal places it comes out as 0.000, which is useless.
My function will round that number to 0.0004536 instead, if told to do it to 4 significant digits.

You're free to use this function in your own scripts, including closed-source scripts, without asking permission. Credit to @SimpleCryptoLife would be appreciated.

Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.

Want to use this script on a chart?