It is currently April 25th, 2024, 6:50 am

Help with adding prefix to positive number

Get help with creating, editing & fixing problems with skins
364def49db
Posts: 9
Joined: December 19th, 2021, 3:12 am

Help with adding prefix to positive number

Post by 364def49db »

Good evening Rainmeter forums,

I'm having an issue adding a prefix if a measurement is a positive number. I have the negative number from a WebPaser so I do not need to display the "-" sign but if the number is positive I would like the meter to display a "+" sign. It has to be something simple I would think, I just can't wrap my head around it or find anything about it.

Code: Select all

[MeasureGlucoseDelta]
Measure=WebParser
URL=[MeasureSite]
StringIndex=6
IfCondition=MeasureGlucoseDelta < 0
IfTrueAction=[!SetOption MeterGlucoseDelta FontColor 220,20,60]
IfCondition2=MeasureGlucoseDelta > 0
IfTrueAction2=[!SetOption MeterGlucoseDelta FontColor 125,252,0]
DynamicVariables=1

# THIS IS FOR TESTING
[MeasureDeltaSign]
Measure=WebParser
URL=[MeasureSite]
StringIndex=6
IfCondition=MeasureDeltaSign < 0
IfTrueAction="NEG"
IfCondition2=MeasureDeltaSign > 0
IfTrueAction2="POS"
DynamicVariables=1

[meterGlucoseDelta]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureGlucoseDelta
MeasureName2=MeasureDeltaSign
X=1
Y=25
W=100
H=20
Text=%2[&MeasureGlucoseDelta]
DynamicVariables=1
User avatar
SilverAzide
Rainmeter Sage
Posts: 2605
Joined: March 23rd, 2015, 5:26 pm

Re: Help with adding prefix to positive number

Post by SilverAzide »

364def49db wrote: October 7th, 2022, 2:28 am I'm having an issue adding a prefix if a measurement is a positive number. I have the negative number from a WebPaser so I do not need to display the "-" sign but if the number is positive I would like the meter to display a "+" sign. It has to be something simple I would think, I just can't wrap my head around it or find anything about it.
There are probably many ways to handle this. Maybe something like this? (I did not try it.)

Code: Select all

[MeasureGlucoseDelta]
Measure=WebParser
URL=[MeasureSite]
StringIndex=6

[CalcGlucoseDelta]
Measure=Calc
Formula=[MeasureGlucoseDelta]
IfCondition=CalcGlucoseDelta < 0
IfTrueAction=[!SetOption MeterGlucoseDelta FontColor 220,20,60][!SetOption MeterGlucoseDelta Text "%1"][!UpdateMeter MeterGlucoseDelta][!Redraw]
IfCondition2=CalcGlucoseDelta > 0
IfTrueAction2=[!SetOption MeterGlucoseDelta FontColor 125,252,0][!SetOption MeterGlucoseDelta Text "+%1"][!UpdateMeter MeterGlucoseDelta][!Redraw]
DynamicVariables=1

[MeterGlucoseDelta]
Meter=String
MeterStyle=styleLeftText
MeasureName=MeasureGlucoseDelta
X=1
Y=25
W=100
H=20
Text="%1"
I split out the IfConditions into a separate Calc measure because the MeasureGlucoseDelta measure is going to briefly return an empty string while the data is being fetched from the web, so I think the IfConditions will all fail with a syntax error. Once the data arrives, the error will go away, but this might be one way to prevent that (there are other ways too). If you don't care about errors, then you can combine the measures back the way you had it. So this is similar to what you had, just adding some SetOption bangs to change the Text option on the meter.
Gadgets Wiki GitHub More Gadgets...
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with adding prefix to positive number

Post by balala »

364def49db wrote: October 7th, 2022, 2:28 am I'm having an issue adding a prefix if a measurement is a positive number. I have the negative number from a WebPaser so I do not need to display the "-" sign but if the number is positive I would like the meter to display a "+" sign. It has to be something simple I would think, I just can't wrap my head around it or find anything about it.
The IfTrueAction and IfTrueAction2 options of the [MeasureDeltaSign] measure do nothing. In Rainmeter the IfTrueAction="NEG"
and IfTrueAction2="POS" options don't work, they do nothing. In these options you have to use bangs.
See SilverAzide's reply and sample code above, which is perfect. However, I'd like to show another approach as well. This doesn't require IfConditions, but uses substitution instead. I'll show what am I thinking to, on the [MeasureDeltaSign] measure. I assume (hope) this measure is always returning integer value.
So, if you want to try my solution, remove the IfConditions and the appropriate IfTrueActions from the measure and add the following two options:

Code: Select all

[MeasureDeltaSign]
...
RegExpSubstitute=1
Substitute="^(\d*)$":"+\1"
With these options the measure returns the negative number as it is, but if there is no sign before the number, it adds a + in front of it.
Note that this solution alters the string value of the [MeasureDeltaSign] measure, even if doesn't affect its numeric value. If its string value is used anywhere in your code, this solution can't be used, or at least has to be modified.
emp00
Posts: 83
Joined: October 7th, 2022, 8:08 pm

Re: Help with adding prefix to positive number

Post by emp00 »

Dear All,

@364def49db many thanks for your efforts, I'm the originator of the request to your excellent NS2Win rainmeter skin :-)

Success! Thanks so much @SilverAzide, your code works perfectly. See screenshot below. @balala, I also tried your proposal but this has indeed the disadvantage of showing an error string during each meter update for approx. 1 second. @SilverAzide's code does not have this issue, so I'm sticking with this. THANK YOU!!

Image
User avatar
balala
Rainmeter Sage
Posts: 16168
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Help with adding prefix to positive number

Post by balala »

emp00 wrote: October 8th, 2022, 12:05 pm @balala, I also tried your proposal but this has indeed the disadvantage of showing an error string during each meter update for approx. 1 second. @SilverAzide's code does not have this issue, so I'm sticking with this.
As you want. From my part I'm glad if you got it working as expected.
364def49db
Posts: 9
Joined: December 19th, 2021, 3:12 am

Re: Help with adding prefix to positive number

Post by 364def49db »

Thanks all! This works great! Thanks for the help on this!

emp00 wrote: October 8th, 2022, 12:05 pm Dear All,

@364def49db many thanks for your efforts, I'm the originator of the request to your excellent NS2Win rainmeter skin :-)

Success! Thanks so much @SilverAzide, your code works perfectly. See screenshot below. @balala, I also tried your proposal but this has indeed the disadvantage of showing an error string during each meter update for approx. 1 second. @SilverAzide's code does not have this issue, so I'm sticking with this. THANK YOU!!

Image