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.

Skript med en öppen källkod

I sann TradingView-anda har författaren publicerat detta skript med öppen källkod så att andra handlare kan förstå och verifiera det. Hatten av för författaren! Du kan använda det gratis men återanvändning av den här koden i en publikation regleras av våra ordningsregler. Du kan ange den som favorit för att använda den i ett diagram.

Frånsägelse av ansvar

Informationen och publikationerna är inte avsedda att vara, och utgör inte heller finansiella, investerings-, handels- eller andra typer av råd eller rekommendationer som tillhandahålls eller stöds av TradingView. Läs mer i Användarvillkoren.

Vill du använda det här skriptet i ett diagram?