It is currently March 29th, 2024, 11:21 am

Format a number with commas using RegExpSubstitute

Tips and Tricks from the Rainmeter Community
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Format a number with commas using RegExpSubstitute

Post by eclectic-tech »

Mor3bane wrote:hi, Thanks eclectictech

You realised right away what I was aiming for :D

However, returning the Trunc seems a bit overworked, doesn't it?

Wouldn't there be a capture for the "." with an escape in the Substitute?

I've tried the couple ways I thought would work but both seem to break the nifty addition of commas that attracted me to this thread.

Edit: Also in trying your suggestion, I note that the pop value starts at zero, then resets at 800?
You could change the RegExp to only capture the integer portion from the website... then you would not need worry about Trunc or Round. But you have to use a Calc measure to add a loop value, which simulate website activity between parses, so adding the action to change it to an integer is not in anyway 'overworked'. :uhuh:

The way my posted code works, I am adding the loop value to the pop value, the pop value is only parsed every 5 minutes from the website (default UpdateRate for the Webparser). This addition simulates what the website is doing without having to hit the site 3 times a second. The pop value is increased by the loop value, then readjusted every 5 minutes by getting the new value from the website.

The update rate of the skin (~390 ms) allows the skin to show a fairly accurate increase of the pop value, while not actually parsing the website constantly. The loop measure has to reset to zero when the new value is parsed, and the pop value in the skin is 'adjusted' to match the website. At that point, the new website value begins to be increased by the loop measure in the skin again beginning at zero. :x

Because a new value is received from the website every 5 minutes, the 800 value in the loop is enough to provide values to simulate the value the website would have, until it is parsed again. You'll notice it usually only gets to ~600 between updates.

Feel free to use this or not... there are many ways get this done, this was just my suggestion. :welcome:
User avatar
Mor3bane
Posts: 943
Joined: May 7th, 2016, 7:32 am

Re: Format a number with commas using RegExpSubstitute

Post by Mor3bane »

Hey, sorry if I inferred anything unfriendly by using "overworked" - I meant more to the tune of "I didn't understand the code".

:???:

Of the other ways, is there a substitute solution for ignoring everything beyond and including the decimal dot?

I'm not really worried if the meter does not update in simulated real time.

8-)
My DevArt Gallery

There are many ways to be different - there is only one way to be yourself - be amazing at it

The law of averages says what it means; even if you get everything right, you will get something wrong. Therefore; self managing error trapping initiates another set of averages - amongst the errors, some of them will not be errors, instead those instances will appear to be "luck". One cannot complain of the 'appearance' of 'infinite regress of causation', even if it does not have a predictable pattern, only that it requires luck to achieve.
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5384
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Format a number with commas using RegExpSubstitute

Post by eclectic-tech »

Mor3bane wrote:Hey, sorry if I inferred anything unfriendly by using "overworked" - I meant more to the tune of "I didn't understand the code".
No problem about the 'overworked' term, sometimes I read too much into things that are typed :D
Mor3bane wrote:... Of the other ways, is there a substitute solution for ignoring everything beyond and including the decimal dot? ...
To only capture the integer portion from the website, modify the RegExp:
RegExp="(?siU).*<p class='counter' id='counters_number_interval_5'>.*number_interval\((.*)\..*"
This will capture up to the decimal point and not return the fractional portion.

Let me(us) know if you have any unanswered issues. :great:
User avatar
Mor3bane
Posts: 943
Joined: May 7th, 2016, 7:32 am

Re: Format a number with commas using RegExpSubstitute

Post by Mor3bane »

eclectic-tech wrote: To only capture the integer portion from the website, modify the RegExp:
RegExp="(?siU).*<p class='counter' id='counters_number_interval_5'>.*number_interval\((.*)\..*"
This will capture up to the decimal point and not return the fractional portion.

Let me(us) know if you have any unanswered issues. :great:
Wow, I even tried something similar, only in the Substitute...

So knowing just a tad, I fixed the Substitute from:
Substitute="\d{1,3}(?=(\d{3})+\.)":"\0,"
To:
Substitute="\d{1,3}(?=(\d{3}))":"\0,"
And the nice commas are working as well!

Thanks eclectic-tech :thumbup: :rosegift:

(I appreciate you're time :great: )

Edit: The Substitute should look like this:
Substitute="\d{1,3}(?=(\d{3})+$)":"\0,"
My DevArt Gallery

There are many ways to be different - there is only one way to be yourself - be amazing at it

The law of averages says what it means; even if you get everything right, you will get something wrong. Therefore; self managing error trapping initiates another set of averages - amongst the errors, some of them will not be errors, instead those instances will appear to be "luck". One cannot complain of the 'appearance' of 'infinite regress of causation', even if it does not have a predictable pattern, only that it requires luck to achieve.