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

How can I format money?

Get help with creating, editing & fixing problems with skins
bkuhl
Posts: 3
Joined: August 27th, 2017, 12:49 pm

How can I format money?

Post by bkuhl »

I'm attempting to take a number like 5342.9 and would like to format it into USD. Ideally the outcome would be $5,342.90. However to start, I'm attempting to prepend the dollar sign to the value. My Measure is:

Code: Select all

    [MeasureBTCUSDValue]
    Measure=Plugin
    Plugin=WebParser
    URL=[MeasureBTC]
    Substitute="(.?+)":"$\1"
    StringIndex=1
Whether I escape the `$` or not in the replace value, I end up with 5342.9. What is incorrect about my regex that's leading to the USD sign not being prepended? Is there a better way to handle money formatting in Rainmeter?
bkuhl
Posts: 3
Joined: August 27th, 2017, 12:49 pm

Re: How can I format money?

Post by bkuhl »

I can get around this by using

Code: Select all

[MeasureUSDValue]
Measure=Plugin
Plugin=WebParser
URL=[MeasureBTC]
StringIndex=1

[MeterColumnValue]
Meter=String
MeasureName=MeasureUSDValue
Text=$%1
which adds the $ when the number is displayed. This doesn't solve the decimal places or the comma if the value is in the thousands.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can I format money?

Post by jsmorley »

This is actually kinda tricky in Rainmeter proper, as I assume you not only want to round and force the number to two decimal places, and put a "$" in front, but want to format the number with appropriate "commas" to separate thousands. That is going to be complicated at best with just a regular expression Substitute.

What I would recommend is some Lua.

I went and absconded with a library of some .lua from here:

http://lua-users.org/wiki/FormattingNumbers

And saved that as FormatNumbers.lua in the @Resources folder of the skin.
I can then load that in my Lua with a dofile() function in the Initialize() portion of the script.

Here is the .rmskin you can download and tear apart:
FormatMoney_1.0.rmskin
FormatMoney.ini

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1
OnRefreshAction=[!UpdateMeasure MeasureCalc]

[MeasureScript]
Measure=Script
ScriptFile=FormatMoney.lua
Disabled=1

; Generate some random number that can be in the thousands and is certain to have decimal places
[MeasureCalc]
Measure=Calc
Formula=Random / 2.51
UpdateRandom=1
LowBound=200
HighBound=20000
UpdateDivider=2
OnChangeAction=[!CommandMeasure MeasureScript "FormatMoney([MeasureCalc])"]

[MeterMoney]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
FormatMoney.Lua:

Code: Select all

function Initialize()

	dofile(SKIN:GetVariable('@')..'FormatNumbers.lua')

end

function FormatMoney(inArg)

	outString = format_num(inArg,2,"$")
	
	SKIN:Bang('!SetOption', 'MeterMoney', 'Text', outString)
	SKIN:Bang('!UpdateMeter', 'MeterMoney')
	SKIN:Bang('Redraw')

end
1.png
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How can I format money?

Post by jsmorley »

If you get the latest 4.1 beta of Rainmeter from https://www.rainmeter.net, it can even be a bit simpler using Inline Lua:

Skin:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[MeasureScript]
Measure=Script
ScriptFile=FormatMoney.lua
Disabled=1

[MeasureCalc]
Measure=Calc
Formula=Random / 2.51
UpdateRandom=1
LowBound=200
HighBound=20000
UpdateDivider=2
OnUpdateAction=[!CommandMeasure MeasureScript "FormatMoney([MeasureCalc])]

[MeterMoney]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=Amount is: [&MeasureScript:moneyFormat]
Lua:

Code: Select all

function Initialize()

	dofile(SKIN:GetVariable('@')..'FormatNumbers.lua')

end	

function FormatMoney(inArg)

	moneyFormat = format_num(inArg,2,"$")

end
1.png
You do not have the required permissions to view the files attached to this post.
bkuhl
Posts: 3
Joined: August 27th, 2017, 12:49 pm

Re: How can I format money?

Post by bkuhl »

I do have the 4.1 beta. I really appreciate the quick response! This is really helpful. I'll tinker with this later today when I get the chance.