TradingView
SimpleCryptoLife
8 Okt 2021 pukul 12.35

SignificantFigures 

Bitcoin / TetherUSBinance

Huraian

Library "SignificantFigures"

sigFig(float _float, int _figures)

@description 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 digits left. 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.
I think this is more friendly.

@Function Converts float with arbitrary number of digits to one with a specified number of significant figures.
@param float _float is the floating-point number to manipulate.
@param int _figures is the number of significant figures you want.
@Returns Returns a float with the specified number of significant figures

Nota Keluaran

Update: Adjusted to cope with an arbitrary number of significant figures instead of hardcoded 10.

Nota Keluaran

v3 is completely rewritten to use the new string manipulation functions instead of arrays. This makes it much shorter and easier to understand. It also fixed a horrendous bug with the previous versions.
EVERYONE SHOULD UPGRADE to v3 for this reason.

Nota Keluaran

v4
Completely new logic, using maths to do maths instead of messing around with strings.
Credit to @tepmoc for supplying the function that showed me how to do it.
Komen
JohnBaron
nice work on this library function! Sharing it with @kurtsmock
SimpleCryptoLife
@JohnBaron, Thank you :-)
SimpleCryptoLife
Forgot to mention that for v3 and all previous versions, this function only truncates the number, it does not do any rounding. So if given an input of 0.00045366666666666666666666667, and a significant figure number of 4, it will show 0.0004536 and not 0.0004537. Feel free to extend it to do this if you have the time.
tepmoc
@SimpleCryptoLife, round_sig(source, sig) =>
math.round(source, sig-int(math.floor(math.log10(math.abs(source))))-1)
SimpleCryptoLife
@tepmoc, Thanks! I've rewritten the library to use your function for numbers with a decimal point. And a different one for numbers without.
tepmoc
@SimpleCryptoLife, i suggest you to leave feedback to tradingview, so they change math.round to accept negative numbers for precision to round integers, that way single formula work on everything.
SimpleCryptoLife
@tepmoc, Good plan!
Lebih