Calculating number of decimals - how?

Questions about MultiCharts and user contributed studies.
User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Calculating number of decimals - how?

Postby JoshM » 22 Jun 2012

I'm trying to calculate the number of decimals in a variable, so that I can use NumToStr to print these with all decimals shown. Since I don't know beforehand how many decimals end up in the variable, I need to calculate this instead of using a fixed value.

A way to do this would be to convert the number to a string, and count the number of characters following the dot ("."). However, to convert a number to a string in PowerLanguage, you'll need NumToStr(), which brings me back at the beginning of this problem. :) Who knows a mathematical way to achieve this?

Code: Select all

Variables:
   myVariable(0),
   decimalsOfVariable(0);
   
if (LastBarOnChart_s = True) then begin
   
   myVariable = 123.456789123;
   
   // Get number of decimals
   // ?

   Print("My variable: ", NumToStr(myVariable, decimalsOfVariable));
end;   

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Calculating number of decimals - how?

Postby TJ » 22 Jun 2012

JoshM wrote:I'm trying to calculate the number of decimals in a variable, so that I can use NumToStr to print these with all decimals shown. Since I don't know beforehand how many decimals end up in the variable, I need to calculate this instead of using a fixed value.

A way to do this would be to convert the number to a string, and count the number of characters following the dot ("."). However, to convert a number to a string in PowerLanguage, you'll need NumToStr(), which brings me back at the beginning of this problem. :) Who knows a mathematical way to achieve this?

Code: Select all

Variables:
   myVariable(0),
   decimalsOfVariable(0);
   
if (LastBarOnChart_s = True) then begin
   
   myVariable = 123.456789123;
   
   // Get number of decimals
   // ?

   Print("My variable: ", NumToStr(myVariable, decimalsOfVariable));
end;   


I thought we have already gone through this?

viewtopic.php?f=1&t=9164

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Calculating number of decimals - how?

Postby JoshM » 22 Jun 2012

TJ wrote:I thought we have already gone through this?

viewtopic.php?f=1&t=9164

That's true TJ, but if I apply your suggestion in that thread to this problem, I don't get the number of decimals with a division through log(10):

Code: Select all

Variables:
   myVariable(0),
   decimalsOfVariable(0);
   
if (LastBarOnChart_s = True) then begin
   
   once cleardebug;
   
   myVariable = 123.456789123;
   
   // Get number of decimals
   decimalsOfVariable = Log(myVariable) / Log(10);

   Print("Decimals of variable: ", NumToStr(decimalsOfVariable, 5), NewLine,
         "My variable: ", NumToStr(myVariable, decimalsOfVariable));
end;   

Code: Select all

Decimals of variable: 2.09151
My variable: 123.46


I'm not seeing how the "Log(PriceScale) / Log(10)" can be applied to this problem, since PriceScale is (as far as I know) always an integer value.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Calculating number of decimals - how?

Postby TJ » 22 Jun 2012

JoshM wrote:
TJ wrote:I thought we have already gone through this?

viewtopic.php?f=1&t=9164

That's true TJ, but if I apply your suggestion in that thread to this problem, I don't get the number of decimals with a division through log(10):
...
I'm not seeing how the "Log(PriceScale) / Log(10)" can be applied to this problem, since PriceScale is (as far as I know) always an integer value.


I have mistaken your problem.

My formula is for finding the decimal place of the data series, not for variable.

There is a solution, let me dig it up.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Calculating number of decimals - how?

Postby TJ » 22 Jun 2012

JoshM wrote:I'm trying to calculate the number of decimals in a variable, so that I can use NumToStr to print these with all decimals shown. Since I don't know beforehand how many decimals end up in the variable, I need to calculate this instead of using a fixed value.

A way to do this would be to convert the number to a string, and count the number of characters following the dot ("."). However, to convert a number to a string in PowerLanguage, you'll need NumToStr(), which brings me back at the beginning of this problem. :) Who knows a mathematical way to achieve this?
...


NumToStr is the preferred way,
because a mathematical way (eg multiply by 10) can introduce inaccuracies when the number gets processed back and forth multiple times.

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Calculating number of decimals - how?

Postby JoshM » 22 Jun 2012

TJ wrote:NumToStr is the preferred way,
because a mathematical way (eg multiply by 10) can introduce inaccuracies when the number gets processed back and forth multiple times.

Thanks TJ, but I don't see how this might work since we don't know beforehand how many decimals a variable will have, so which parameter needs to be entered for NumToStr()?

If we'd use a high value, say 20, then we're pretty sure the whole number will gets printed. For example:

Code: Select all

Variables:
   myVariable(0), mySecondVar(0),
   decimalsOfVariable(0);
   
if (LastBarOnChart_s = True) then begin
   
   once cleardebug;
   
   myVariable    = 123.456789123;
   mySecondVar   = 9.80900;
   
   Print("My variable 1: ", NumToStr(myVariable, 20));
   Print("My variable 2: ", NumToStr(mySecondVar, 20));

end;


Now if we want to determine the number of decimals by counting the numbers behind the dot, we'll need to "cut off" all zeroes (in which case the number of decimals of 'mySecondVar' is underestimated with two). And secondly, NumToStr() doesn't always give a clean output. Look for example at the print out from the variables:

Code: Select all

My variable 1: 123.45678912299999000000
My variable 2: 9.80899999999999930000


So I don't think this is a solution either.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Calculating number of decimals - how?

Postby TJ » 22 Jun 2012

JoshM wrote:
TJ wrote:NumToStr is the preferred way,
because a mathematical way (eg multiply by 10) can introduce inaccuracies when the number gets processed back and forth multiple times.

Thanks TJ, but I don't see how this might work since we don't know beforehand how many decimals a variable will have, so which parameter needs to be entered for NumToStr()?
....


I meant to use NumToStr to convert the number to string, then find the position of the dot.

User avatar
JoshM
Posts: 2080
Joined: 20 May 2011
Location: The Netherlands
Has thanked: 1525 times
Been thanked: 1483 times
Contact:

Re: Calculating number of decimals - how?

Postby JoshM » 22 Jun 2012

JoshM wrote:we don't know beforehand how many decimals a variable will have, so which parameter needs to be entered for NumToStr()?


TJ wrote:I meant to use NumToStr to convert the number to string, then find the position of the dot.

Yes that's also what I meant, but that isn't possible from my understanding.

How can NumToStr be used to convert a variable to a string if one doesn't know the number of decimals of the variable? And the default Text() can't be used here since Text() converts with a default of two decimals.

NumToStr(Expression, Dec)

Where:

Expression - a numerical expression to be converted to a string expression.

Dec - a numerical expression specifying the number of decimal places the returned string expression is to contain.

User avatar
TJ
Posts: 6551
Joined: 29 Aug 2006
Location: Global Citizen
Has thanked: 966 times
Been thanked: 1892 times

Re: Calculating number of decimals - how?

Postby TJ » 22 Jun 2012

JoshM wrote:
JoshM wrote:we don't know beforehand how many decimals a variable will have, so which parameter needs to be entered for NumToStr()?


TJ wrote:I meant to use NumToStr to convert the number to string, then find the position of the dot.

Yes that's also what I meant, but that isn't possible from my understanding.

How can NumToStr be used to convert a variable to a string if one doesn't know the number of decimals of the variable? And the default Text() can't be used here since Text() converts with a default of two decimals.

NumToStr(Expression, Dec)

Where:

Expression - a numerical expression to be converted to a string expression.

Dec - a numerical expression specifying the number of decimal places the returned string expression is to contain.


You must first determine the precision you want to work with.
If you don't, the decimal size can get astronomical,
and the value becomes insignificant even if you have the precision.


Return to “MultiCharts”