Creating a string variable from a number variable

Post your questions and help other users.

Moderator: Martin

Post Reply
skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Creating a string variable from a number variable

Post by skiptannen » 09 Nov 2017 18:39

<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.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Creating a string variable from a number variable

Post by Martin » 09 Nov 2017 20:33

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

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Creating a string variable from a number variable

Post by skiptannen » 10 Nov 2017 12:56

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

User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Re: Creating a string variable from a number variable

Post by yogi108 » 10 Nov 2017 13:36

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,
"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

bogdyro
Posts: 241
Joined: 04 Apr 2015 15:14

Re: Creating a string variable from a number variable

Post by bogdyro » 10 Nov 2017 13:37

Hi. What is the purpose of having for instance a string '12'. Could you please provide the script?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Creating a string variable from a number variable

Post by Desmanto » 10 Nov 2017 13:50

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)
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.

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Creating a string variable from a number variable

Post by skiptannen » 10 Nov 2017 15:08

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... :D

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!

Post Reply