Page 1 of 2
Format a number with commas using RegExpSubstitute
Posted: December 30th, 2013, 2:19 pm
by jsmorley
Just in case you want to format a number with commas for display in a meter, it can be done with RegExpSubtitute / Substitute.
This will only work with positive whole numbers of virtually any size, up to whatever is supported in a Calc / Formula, some monstrous 64bit integer or something. Be sure to Round / Ceil / Floor / Trunc any numbers that might contain decimal places.
Code: Select all
[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
[MeasureNumber]
Measure=Calc
Formula=Round(83902137402495.51)
RegExpSubstitute=1
Substitute="^(\d{1,2}?)((\d{3})+)$":"\1,\2","(\d{3})(?=\d)":"\1,"
[MeterString]
Meter=String
FontSize=13
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=[MeasureNumber:]#CRLF#[MeasureNumber]
DynamicVariables=1
2013-12-30_091700.jpg
What it does:
Looks ahead optionally for either 1 or 2 numbers, followed by a repeating pattern of 3 numbers and the end of the string. That will get the beginning part of the string, assuming that it is a number like 9,000 or 99,000. It adds a comma after that pattern. If there are no patterns of 3 numbers after the first capture group, the number is less than 1,000 and nothing is done. So at the end of the first part of the substitution in our example, we end up with:
83,902137402495
Then it takes that result, and looks ahead to add commas after every repeating pattern of 3 numbers excepting the one that ends the string (it knows that by looking ahead after capturing each group of three to see if there are any more numbers to come (?=\d)):
83,902,137,402,495
Re: Format a number with commas using RegExpSubstitute
Posted: December 30th, 2013, 2:49 pm
by moshi
if you do round, a shorter one should do:
Code: Select all
Substitute="\d{1,3}(?=(\d{3})+$)":"\0,"
Re: Format a number with commas using RegExpSubstitute
Posted: December 30th, 2013, 2:53 pm
by jsmorley
moshi wrote:if you do round, a shorter one should do:
Code: Select all
Substitute="\d{1,3}(?=(\d{3})+$)":"\0,"
Yeah, looks like that would work as well...
What we really need is one that will do negative numbers and decimal places, but I have not yet found a single regular expression statement that will do that.
Which is about 90% of the reason I posted this at all, to whet your appetite. No Lua, no RunCommand allowed...
Re: Format a number with commas using RegExpSubstitute
Posted: December 31st, 2013, 12:08 am
by moshi
i think i solved this here:
http://rainmeter.net/forum/viewtopic.php?f=5&t=16551&p=92235
it would be a nice challenge to have the whole thing in a single substitution though.
(and also support the Indian numbering system
http://en.wikipedia.org/wiki/Indian_Numbering_System)
Re: Format a number with commas using RegExpSubstitute
Posted: January 4th, 2014, 9:34 am
by moshi
i think i'll have to give up. i can't do it with a single substitution.
Code: Select all
Substitute="\d{1,3}(?=(\d{3})+\.)":"\0,"
works fine for positive/negative numbers with decimal places
Code: Select all
Substitute="\d{1,3}(?=(\d{3})+$)":"\0,"
works fine for positive/negative numbers without decimal places
but i am not able to merge the two together.
i need multiple substitions like this:
Code: Select all
Substitute="^(?(?=-)-)(\d+)$":"\0.","\d{1,3}(?=(\d{3})+\.)":"\0,","\.$":""
to get it working for positive/negative numbers with/without decimal places
if one wants to support German, French, Swiss, etc. formatting and maybe also read such a format (using
, instead of
. for decimal places) with the WebParser plugin, it'll get more complicated:
Code: Select all
[Variables]
Separator=","
Decimal="."
[Measure]
...
Substitute="^(?(?=-)-)(\d+)$":"\0A","([\.,])":"A","\d{1,3}(?=(\d{3})+A)":"\0B","A$":"","A":"#Decimal#","B":"#Separator#"
Re: Format a number with commas using RegExpSubstitute
Posted: January 4th, 2014, 2:47 pm
by jsmorley
Don't worry about it Moshi, it's trivial to do in Lua with something like
http://docs.rainmeter.net/snippets/add-commas.
In any case we have something new coming out tomorrow that will involve regular expression, that you can probably dig your teeth into.
Re: Format a number with commas using RegExpSubstitute
Posted: January 4th, 2014, 3:48 pm
by moshi
not sure if that "trivial" solution (reversing, adding commas, reversing again) doesn't need just as many or more steps than then the three substitutions chain needed for the same result. at least it needs one additional measure.
jsmorley wrote:In any case we have something new coming out tomorrow that will involve regular expression, that you can probably dig your teeth into.
hurry up then
Re: Format a number with commas using RegExpSubstitute
Posted: March 4th, 2017, 7:00 am
by Mor3bane
moshi wrote:i think i'll have to give up. i can't do it with a single substitution.
Code: Select all
Substitute="\d{1,3}(?=(\d{3})+\.)":"\0,"
works fine for positive/negative numbers with decimal places
Code: Select all
Substitute="\d{1,3}(?=(\d{3})+$)":"\0,"
works fine for positive/negative numbers without decimal places
Hi moshi, or jsmorley
I found this very useful indeed.
I am trying to figure out hoe to ignore the decimal value, e.g. the decimal and everything after it.
Anyone care to offer a hand?
I am imagining I already learnt this, but my selective memory is not selecting the correct memory just now
Re: Format a number with commas using RegExpSubstitute
Posted: March 4th, 2017, 1:01 pm
by eclectic-tech
Mor3bane wrote:Hi moshi, or jsmorley
I found this very useful indeed.
I am trying to figure out hoe to ignore the decimal value, e.g. the decimal and everything after it.
Anyone care to offer a hand?
I am imagining I already learnt this, but my selective memory is not selecting the correct memory just now
There are several ways to return integers in a Calc measure; I usually use either
Trunc(x) or
Round(x). ~
Formulas~
Trunc ignores the decimal value, Round will use it to increase/decrease the integer.
Code: Select all
[Rainmeter]
Update=388
AccurateText=1
DynamicWindowSize=1
Group=#RootConfig#
[WorldPop]
Measure=Plugin
Plugin=Plugins\WebParser.dll
;UpdateDivider=600
Url=http://www.theworldcounts.com/counters/shocking_environmental_facts_and_statistics/world_population_clock_live
RegExp="(?siU).*<p class='counter' id='counters_number_interval_5'>.*number_interval\((.*),.*"
;Debug=2
StringIndex=1
FinishAction=[!Log "Current Population Value [#CurrentSection#]"][!CommandMeasure MeasureLoop "Reset"]
[MeasureLoop]
Measure=Loop
StartValue=0
EndValue=800
Increment=1
LoopCount=0
[WorldPopPlus]
Measure=Calc
Formula=Trunc(WorldPop) + MeasureLoop
RegExpSubstitute=1
Substitute="^(\d{1,2}?)((\d{3})+)$":"\1,\2","(\d{3})(?=\d)":"\1,"
DynamicVariables=1
[DummyString]
Meter=String
MeasureName=WorldPopPlus
InlineSetting=Size | 36
InlineSetting2=Color | 225,225,225
Edit: Added the RegExpSubstitute to the Calc measure...
Re: Format a number with commas using RegExpSubstitute
Posted: March 4th, 2017, 7:03 pm
by Mor3bane
hi, Thanks eclectictech
You realised right away what I was aiming for
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?