<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.
Creating a string variable from a number variable
Moderator: Martin
-
- Posts: 82
- Joined: 13 Jan 2014 21:39
Re: Creating a string variable from a number variable
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
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
-
- Posts: 82
- Joined: 13 Jan 2014 21:39
Re: Creating a string variable from a number variable
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.
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
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,
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,
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga
Shri Mataji Nirmala Devi, founder of Sahaja Yoga
Re: Creating a string variable from a number variable
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
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,
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)
You must put only
x = "12"
try this,
Code: Select all
x = "12";
y = isString(x);
z = x + 3;
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)
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.
-
- Posts: 82
- Joined: 13 Jan 2014 21:39
Re: Creating a string variable from a number variable
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...Desmanto wrote:To sense if {x} is probably a number, then it will try to convert that to number first...

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";
Cheers!