How to convert a number to a string and format it so that each character is appended with a /n

SleepyZ

Well-known member
Example:
Convert a Number to String:
123 to "1" + "/n" + "2" + "/n"+"3"+"/n"
Not exactly sure what you want, but here is an addchartbubble with the 123 converted to a stacked 123.

Ruby:
plot Data = expaverage(close,8) crosses close;
addchartBubble(data,high,"1\n2\n3", color = Color.WHITE);
Screenshot-2021-08-06-184320.png
 

SleepyZ

Well-known member
Not exactly sure what you want, but here is an addchartbubble with the 123 converted to a stacked 123.
Here is another method that creates a label with the hopefully any 3 digit number you input.
Ruby:
input data = 592;
def digit1 = Round(Floor(data / 100), 0);
def digit2 = Floor((data - Round(Floor(data / 100), 0) * 100) / 10);
def digit3 = (data - Round(Floor(data / 100), 0) * 100) / 10 % 1 * 10;

addlabel(1,digit1+"/n"+digit2+"/n"+digit3+"/n",color.white);
 

Joshua

Active member
Holy crap that is actually quite impressive. I guess I prematurely resigned myself to just accepting that ThinkScript has a total lack of support for strings, and never even bothered to attempted a mathematical approach like that.
 

ksadras

New member
VIP
Here is another method that creates a label with the hopefully any 3 digit number you input.
Works perfectly thanks. Is there a way to dynamically convert a double to a string and then process? For example if the close = 4268.32 then I need to know that it has 4 digits to process instead of 3. Also why is it I cannot assign string to AsText. Input string1 = AsText(....) doesn't work.
 

SleepyZ

Well-known member
Works perfectly thanks. Is there a way to dynamically convert a double to a string and then process? For example if the close = 4268.32 then I need to know that it has 4 digits to process instead of 3. Also why is it I cannot assign string to AsText. Input string1 = AsText(....) doesn't work.
TOS's string functionality is limited.

You cannot input string1 = AsText(...). Look up that function in the education tab for a further explanation of its use.

You can input text as part of a switch/case scenario, for example, input PivotType = {Woodies, Floor, default Camarilla};.

The following code has an example of how to use an input to create text in a label.

The other example helps to determine if the input 'c' value is exactly a 4 digit number.

Ruby:
input x = 1;
AddLabel(1, if x == 1 then "Hello" else "Goodbye", Color.WHITE);
input c = 4268.32;
AddLabel(1, if Between(c / 1000, 1, 9) then "4 digits" else "Not 4 digits" , Color.WHITE);
 

ksadras

New member
VIP
TOS's string functionality is limited.

You cannot input string1 = AsText(...). Look up that function in the education tab for a further explanation of its use.

You can input text as part of a switch/case scenario, for example, input PivotType = {Woodies, Floor, default Camarilla};.

The following code has an example of how to use an input to create text in a label.

The other example helps to determine if the input 'c' value is exactly a 4 digit number.
Thanks a lot. Perfect example for me to extend and make it work.
 

halcyonguy

Well-known member
VIP
TOS's string functionality is limited.

You cannot input string1 = AsText(...). Look up that function in the education tab for a further explanation of its use.

You can input text as part of a switch/case scenario, for example, input PivotType = {Woodies, Floor, default Camarilla};.

The following code has an example of how to use an input to create text in a label.

The other example helps to determine if the input 'c' value is exactly a 4 digit number.
thanks for sharing , sleepyz. i forget to use between.
here is a mod of post#6 , to allow the user to pick the quantity of digits

Ruby:
#
input quantity_of_digits = 4;
def powernum = (quantity_of_digits-1);
def c = close;
def p = power(10, powernum);
#addlabel(1, p, color.magenta);
#AddLabel(1, if Between(c / 1000, 1, 9) then "4 digits" else "Not 4 digits" , Color.WHITE);
AddLabel(1, if Between(c / p, 1, 9) then (quantity_of_digits + " digits") else ("Not " + quantity_of_digits + " digits") , Color.WHITE);
#


Ruby:
## determine quantity of digits in price
def digits = if Between(c / 1, 1, 9) then 1
else if Between(c / 10, 1, 9) then 2
else if Between(c / 100, 1, 9) then 3
else if Between(c / 1000, 1, 9) then 4
else if Between(c / 10000, 1, 9) then 5
else 0;
addlabel(1, "digits in price " + digits, color.yellow);
 
Last edited:

Similar threads

Top