It is currently April 19th, 2024, 4:43 pm

How can I convert a string to a number?

Get help with creating, editing & fixing problems with skins
User avatar
SilverAzide
Rainmeter Sage
Posts: 2603
Joined: March 23rd, 2015, 5:26 pm

Re: How can I convert a string to a number?

Post by SilverAzide »

yamato wrote: June 8th, 2021, 6:56 pm ... and this works not as expected (1.0 is displayed, instead of 1234.5)
Try not scaling the number (the "/1" part). I think all you need is Text=[MeasureString:1].
Gadgets Wiki GitHub More Gadgets...
yamato
Posts: 5
Joined: June 7th, 2021, 8:35 pm

Re: How can I convert a string to a number?

Post by yamato »

I just tried what you suggest (Text=[MeasureString:1]), but i get the same result (1.0 is displayed, instead of 1234.5)
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: How can I convert a string to a number?

Post by death.crafter »

yamato wrote: June 9th, 2021, 5:24 am I just tried what you suggest (Text=[MeasureString:1]), but i get the same result (1.0 is displayed, instead of 1234.5)
You got to convert the string from the string measure to a number before rounding it, because as JSMorley pointed out here, the default number value of a string measure is 1 if it's not a number(which is the case here cause of the "," in your number).

Code: Select all

[MeasureString]
Measure=String
String=1,234.5678
UpdateDivider=-1
Substitute=",":""

[MeasureNum]
Measure=Calc
Formula=[MeasureString]
UpdateDivider=-1

[MeterText]
Meter=String
X=10
Y=10
Text=[MeasureNum:1]
FontFace=Cascadia Code
AntiAlias=1
FontColor=FFFFFF
DynamicVariables=1
Image:
Screenshot 2021-06-09 130940.png
You do not have the required permissions to view the files attached to this post.
from the Realm of Death
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How can I convert a string to a number?

Post by Yincognito »

yamato wrote: June 9th, 2021, 5:24 am I just tried what you suggest (Text=[MeasureString:1]), but i get the same result (1.0 is displayed, instead of 1234.5)
In addition to death.crafter's solution, you could avoid creating a new measure for that and just add OnUpdateAction=[!SetVariable MyNumber (Round([MeasureString],1))] to your [MeasureString], followed by displaying the new variable instead, in [Index], like Text=#MyNumber#.

Reference here.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
yamato
Posts: 5
Joined: June 7th, 2021, 8:35 pm

Re: How can I convert a string to a number?

Post by yamato »

thx again for your quick answers !

Your solution death.crafter works indeed for my simplified problem thx !! (1234.5 is displayed)

However, when applied to my reel widget (Markets Prices by Mordasius), where the string is not defined as a String, but instead as a WebParser, it doesn't work anymore (0.0 is displayed, instead of 36758.7) :

Code: Select all

[InfoIndex4]
Measure=WebParser
URL=https://www.cnbc.com/quotes/#Symbol4#
UserAgent=#MyUserAgent#
Header=Accept: text/html
Header2=Accept_Encoding: gzip, deflate, br
Header3=Accept_Language: en-US
Header4=Connection: close
Header5=Dnt: 1
Header6=Host: www.CNBC.com
Header7=Referer: https://wwww.google.com
RegExp=#ExpQuote#
StringIndex=1
UpdateRate=#UpdateQuotes#

[mIndex4_Price]
Measure=WebParser
Url=[InfoIndex4]
StringIndex=2
Substitute=",":""

[mIndex4_PriceNum]
Measure=Calc
Formula=[mIndex4_Price]
UpdateDivider=-1

[LabelIndex4_Price]
Meter=STRING
X=10
Y=10
Text=[mIndex4_PriceNum:1]
DynamicVariables=1
Would you have more clue about this slightly different problem ?
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How can I convert a string to a number?

Post by Yincognito »

yamato wrote: June 10th, 2021, 6:42 pmHowever, when applied to my reel widget (Markets Prices by Mordasius), where the string is not defined as a String, but instead as a WebParser, it doesn't work anymore (0.0 is displayed, instead of 36758.7)
Remove UpdateDivider=-1 and add DynamicVariables=1 to [mIndex4_PriceNum] and it will work. The former allows the measure to update more than once at skin refresh (when the WebParser still didn't get its data from the site), and the latter allows the measure to "see" the dynamic (since it was changed by the substitution and its parent WebParser) value of the [mIndex4_Price] section variable:
Result.jpg
(I used the 3rd price here, since the 4th was less than 1000; I also used Mordasius' meter instead of creating another one, since it had the style and all already set)
You do not have the required permissions to view the files attached to this post.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
yamato
Posts: 5
Joined: June 7th, 2021, 8:35 pm

Re: How can I convert a string to a number?

Post by yamato »

Your solutions combined work perfectly !

Really thank you a lot death.crafter and Yincognito !
User avatar
Yincognito
Rainmeter Sage
Posts: 7125
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: How can I convert a string to a number?

Post by Yincognito »

Glad you got it working. ;-)
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth