I'm trying to set up a widget to monitor my Raspberry -Pi running Pi-Hole. It polls a web api for a bunch of measures and returns them as strings.
The problem I have is that one of the strings is usually a decimal with up to 6-8 places.
DNS Queries is a whole number
DNS Queries Blocked is a whole number
Percentage of DNS Queries that were Ads is a decimal with up to 6-8 places
What I want is to either:
1. Round that string down to a single decimal place
or
2. Use the other two measures to calculate the third in the format i want.
Thanks for any advice you can offer!
Cheers
It is currently April 19th, 2024, 8:02 pm
How to round a string
-
- Posts: 5
- Joined: February 24th, 2018, 1:12 am
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: How to round a string
If the intent is to display the 3rd number in a String meter, it's pretty simple:quindarious wrote:I'm trying to set up a widget to monitor my Raspberry -Pi running Pi-Hole. It polls a web api for a bunch of measures and returns them as strings.
The problem I have is that one of the strings is usually a decimal with up to 6-8 places.
DNS Queries is a whole number
DNS Queries Blocked is a whole number
Percentage of DNS Queries that were Ads is a decimal with up to 6-8 places
What I want is to either:
1. Round that string down to a single decimal place
or
2. Use the other two measures to calculate the third in the format i want.
Thanks for any advice you can offer!
Cheers
Code: Select all
[MeterString]
Meter=String
MeasureName=MeasureReturningThat3rdNumber
NumOfDecimals=1
-
- Posts: 5
- Joined: February 24th, 2018, 1:12 am
Re: How to round a string
Yep I tried that but had no effect.
this is the code behind it
this is the code behind it
Code: Select all
[AdsPercentageCount]
Meter=String
MeasureName=AdsPercentage
NumOfDecimals=1
X=208
Y=0r
W=180
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: How to round a string
Looks to me like the measure [AdsPercentage] is returning a string value that has the "%" sign already on the end of it.quindarious wrote:Yep I tried that but had no effect.
this is the code behind it
Code: Select all
[AdsPercentageCount] Meter=String MeasureName=AdsPercentage NumOfDecimals=1 X=208 Y=0r W=180 H=12 FontSize=#FontSize# FontColor=#FontColor# FontFace=#Font# Padding=5,5,5,5 StringAlign=Right StringEffect=Shadow AntiAlias=1
This means it is not a "number", and NumOfDecimals won't respond to it.
To fix this, you will need to get that string into a number. First, use Substitute to strip off the "%" sign, then pass the resulting string through a Calc measure to convert it to a real number.
Code: Select all
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
[AdsPercentage]
Measure=String
String=5.032218%
Substitute="%":""
[AdsPercentageNumber]
Measure=Calc
Formula=[AdsPercentage]
DynamicVariables=1
[AdsPercentageCount]
Meter=String
MeasureName=AdsPercentageNumber
NumOfDecimals=1
X=208
Y=0r
W=180
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
Text=%1%
You do not have the required permissions to view the files attached to this post.
-
- Posts: 5
- Joined: February 24th, 2018, 1:12 am
Re: How to round a string
No the number presented by the api doesn't have a percentage sign on the end of it, that gets added afterwards.
This is the output of the api.php webpage
This is the code for displaying the number i'm having issues with
Thanks again for all your help I really appreciate it!
This is the output of the api.php webpage
Code: Select all
{"domains_being_blocked":118745,"dns_queries_today":6518,"ads_blocked_today":326,"ads_percentage_today":5.001534,"unique_domains":549,"queries_forwarded":6089,"queries_cached":103,"clients_ever_seen":2,"unique_clients":2,"status":"enabled"}
Code: Select all
[AdsPercentage]
Measure=Plugin
Plugin=WebParser
URL=[PiholeStats]
StringIndex=4
[AdsPercentageLabel]
Meter=String
X=15
Y=2R
W=190
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringEffect=Shadow
AntiAlias=1
Text=Percent Of Traffic:
[AdsPercentageCount]
Meter=String
MeasureName=AdsPercentage
NumOfDecimals=1
X=208
Y=0r
W=180
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
[AdsPercentageSign]
Meter=String
X=220
Y=0r
W=190
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
SolidColor=0,0,0,1
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
Text=%
Last edited by quindarious on February 24th, 2018, 2:17 am, edited 1 time in total.
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: How to round a string
Note that this would not be needed if the original measure, presumably a WebParser child measure, returned the string without the "%" on the end of it. Then the measure would have both a string and a number value, and the String measure with NumOfDecimals would handle it properly without the need for the Substitute or the intervening Calc measure.
It might be worth looking at the RegExp on the parent WebParser measure, to see if you can get that string sans the "%" sign. As I noted before, you can tack the "%" back on with the Text option on the String meter.
It might be worth looking at the RegExp on the parent WebParser measure, to see if you can get that string sans the "%" sign. As I noted before, you can tack the "%" back on with the Text option on the String meter.
-
- Posts: 5
- Joined: February 24th, 2018, 1:12 am
Re: How to round a string
Yeah there's no percentage sign on the web page
"ads_percentage_today":5.001534,
so i'm not sure why NumOfDecimals=1 isn't working.
"ads_percentage_today":5.001534,
so i'm not sure why NumOfDecimals=1 isn't working.
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: How to round a string
Well, you can remove the Substitute option then, but still pass the string through that Calc measure to turn it into a real number. I thought WebParser child measure that returned a string that was in fact a "number" could directly be used, but it looks like it can't with MeasureName on a String meter. Only with [MeasureName:] as a section variable, but that won't help in this case, as section variables used in the Text option of a String meter won't obey any of the formatting commands like NumOfDecimals or StringCase and such.quindarious wrote:No the number presented by the api doesn't have a percentage sign on the end of it, that gets added afterwards.
This is the output of the api.php webpage
This is the code for displaying the number i'm having issues withCode: Select all
{"domains_being_blocked":118745,"dns_queries_today":6518,"ads_blocked_today":326,"ads_percentage_today":5.001534,"unique_domains":549,"queries_forwarded":6089,"queries_cached":103,"clients_ever_seen":2,"unique_clients":2,"status":"enabled"}
Thanks again for all your help I really appreciate it!Code: Select all
[AdsPercentage] Measure=Plugin Plugin=WebParser URL=[PiholeStats] StringIndex=4 [AdsPercentageLabel] Meter=String X=15 Y=2R W=190 H=12 FontSize=#FontSize# FontColor=#FontColor# FontFace=#Font# Padding=5,5,5,5 StringEffect=Shadow AntiAlias=1 Text=Percent Of Traffic: [AdsPercentageCount] Meter=String MeasureName=AdsPercentage NumOfDecimals=1 X=208 Y=0r W=180 H=12 FontSize=#FontSize# FontColor=#FontColor# FontFace=#Font# Padding=5,5,5,5 StringAlign=Right StringEffect=Shadow AntiAlias=1 [AdsPercentageSign] Meter=String X=220 Y=0r W=190 H=12 FontSize=#FontSize# FontColor=#FontColor# FontFace=#Font# SolidColor=0,0,0,1 Padding=5,5,5,5 StringAlign=Right StringEffect=Shadow AntiAlias=1 Text=%
-
- Posts: 5
- Joined: February 24th, 2018, 1:12 am
Re: How to round a string
You're a superstar! Altered the code as seen below and got the number displayed exactly like I wanted with one decimal place!jsmorley wrote:Well, you can remove the Substitute option then, but still pass the string through that Calc measure to turn it into a real number. I thought WebParser child measure that returned a string that was in fact a "number" could directly be used, but it looks like it can't with MeasureName on a String meter. Only with [MeasureName:] as a section variable, but that won't help in this case, as section variables used in the Text option of a String meter won't obey any of the formatting commands like NumOfDecimals or StringCase and such.
Code: Select all
; Percentage Ads ======================================================================================
[AdsPercentage]
Measure=Plugin
Plugin=WebParser
URL=[PiholeStats]
StringIndex=4
[AdsPercentageLabel]
Meter=String
X=15
Y=2R
W=190
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringEffect=Shadow
AntiAlias=1
Text=Percent Of Traffic:
[AdsPercentageNumber]
Measure=Calc
Formula=[AdsPercentage]
DynamicVariables=1
[AdsPercentageCount]
Meter=String
MeasureName=AdsPercentageNumber
NumOfDecimals=1
X=208
Y=0r
W=180
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
[AdsPercentageSign]
Meter=String
X=220
Y=0r
W=190
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
SolidColor=0,0,0,1
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
Text=%
Last edited by quindarious on February 24th, 2018, 2:34 am, edited 1 time in total.
-
- Developer
- Posts: 22629
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: How to round a string
Oh wait... Doh!
You can do it this way:
You can in fact use a [SectionVariable], but need to first tell it that you want the "number value" with a ":" colon, and follow that with the number of decimal places you want.
Just do that in the Text option, and be sure to add DyanmicVariables=1 to the meter. Remove the MeasureName and NumOfDecimals options entirely.
No Calc measure needed...
https://docs.rainmeter.net/manual/variables/section-variables/#Decimals
You can do it this way:
Code: Select all
[AdsPercentageCount]
Meter=String
X=208
Y=0r
W=180
H=12
FontSize=#FontSize#
FontColor=#FontColor#
FontFace=#Font#
Padding=5,5,5,5
StringAlign=Right
StringEffect=Shadow
AntiAlias=1
Text=[AdsPercentage:1]%
DynamicVariables=1
Just do that in the Text option, and be sure to add DyanmicVariables=1 to the meter. Remove the MeasureName and NumOfDecimals options entirely.
No Calc measure needed...
https://docs.rainmeter.net/manual/variables/section-variables/#Decimals