Page 1 of 1
Creating a string variable from a number variable
Posted: 09 Nov 2017 18:39
by skiptannen
<head banging on wall again> I'm sure this is something incredibly simple and I'll kick myself when I get the answer, and I apologize for taking up the bandwidth for something like this, but...
What I want to do is create a new string variable that contains the text equivalent of a number variable. Basically, I have an existing number var (that can have different values) and I want to create a new string variable that contains the text equivalent of that number. No matter how I write the script the new var ends up as a number var - I just can't get it to be a string. I hope I'm explaining this clearly.
It's always the simple things that makes my head hurt. Thanks in advance.
Re: Creating a string variable from a number variable
Posted: 09 Nov 2017 20:33
by Martin
Hi,
Automagic usually converts strings automatically to numbers when Automagic thinks that it's used in a mathematical context.
In case you want to concatenate numbers together, e.g. 2 + 3 to produce 23, you could use function concat(2, 3) to avoid that the plus sign adds the numbers. If you want to format a number, you can use the numberformat feature:
x = 12.3456;
s = "{x,numberformat,0.00}";//will evaluate s to "12.35"
Please provide a sample script if the concat function or the numberformat feature does not help.
Regards,
Martin
Re: Creating a string variable from a number variable
Posted: 10 Nov 2017 12:56
by skiptannen
Hi Martin,
Thanks very much for the information, but I am trying to do something else. In your example
s is a new
number variable created from
x. What I'm trying to do is create a new
string variable. Take the following script:
x = toNumber("12");
x is a
number variable and I need to create the
string variable
s that contains the text "12".
I hope this makes sense. Thanks again.
Martin wrote:Hi,
Automagic usually converts strings automatically to numbers when Automagic thinks that it's used in a mathematical context.
In case you want to concatenate numbers together, e.g. 2 + 3 to produce 23, you could use function concat(2, 3) to avoid that the plus sign adds the numbers. If you want to format a number, you can use the numberformat feature:
x = 12.3456;
s = "{x,numberformat,0.00}";//will evaluate s to "12.35"
Please provide a sample script if the concat function or the numberformat feature does not help.
Regards,
Martin
Re: Creating a string variable from a number variable
Posted: 10 Nov 2017 13:36
by yogi108
Hi,
Martin gave you already the right answer:
x = toNumber("12");
s = "{x,numberformat,0.00}";//will evaluate s to "12.35"
Just make an action script with these 2 lines above, combine it with a debug condition and try to change the value of s.
It is a string, as you can see there,
Regards,
Re: Creating a string variable from a number variable
Posted: 10 Nov 2017 13:37
by bogdyro
Hi. What is the purpose of having for instance a string '12'. Could you please provide the script?
Re: Creating a string variable from a number variable
Posted: 10 Nov 2017 13:50
by Desmanto
toNumber() is the function to convert a possible string number to number. If you use it this way, x will always be a number.
You must put only
x = "12"
try this,
Code: Select all
x = "12";
y = isString(x);
z = x + 3;
Put debug dialog afterward, you should get y = true and z = 15. Why? How can a variable be a string but still can have math operation at the same time?
Because Automagic behaves just like excel, it has its own intellisense. To sense if {x} is probably a number, then it will try to convert that to number first, before + 3.
So under the hood, that
z = x + 3, has invisible toNumber(), something like
z = toNumber(x) + 3
As Martin stated, if you need a concantenation (you want to get z = "123"), you have to use concat() instead of usual + (plus) sign.
z = concat(x,3)
Re: Creating a string variable from a number variable
Posted: 10 Nov 2017 15:08
by skiptannen
Desmanto wrote:To sense if {x} is probably a number, then it will try to convert that to number first...
Thank you Desmanto - you have cleared up another one for me. The piece that I didn't understand was that AutoMagic will always interpret numbers as numbers even if you try and tell it that number characters are supposed to be interpreted as text. Maybe this is something regex can do, but I have yet to play with it and learn how it works. That's next...
I didn't have a specific script in mind - this was a theoretical exercise. I guess the only way to convert a number to text would be something like:
Code: Select all
x = 12;
y = "{x}" + " is the number 12";
Thanks again, everyone. I'm always learning new things and I'm always amazed by how powerful AutoMagic it is.
Cheers!