Pages: 1
Regex needed for decimal place, how to turn $39.5 into $39.50?
I wondered if anyone might be so kind as to help me with a small template problem I once gave up on but am now giving another try..

I have MyMacros 1.11 and want to use %RegExp with %InputBox so that I can enter several dollar values into a series of %InputBox popups, store them in variables, total them up (I have this part working fine).. then make sure the total is rounded to 2 decimal places before displaying it in the message.

ie. if my total comes out to $39.5 or $295.5, I want it to change it to $39.50 or $295.50 respectively.  But if it's $9.99 for example, I don't want it to do anything.

I'm pretty sure I have to use %RegExp to look for any \d (digit) after a '.' where the next character is null (or anything other than a digit), then append a "0" onto the end of it.. but I think maybe the syntax is confusing me!  :(

Anyone be so kind as to help me out here?

This is all I have so far:

%VARitemA=%InputBox('price of item one: $') %-
%VARitemB=%InputBox('price of item two: $') %-
%VARitemC=%InputBox('price of item three: $') %-
%VARitemD=%InputBox('price of item four: $') %-
total: %calc("%VARitemA + %VARitemB + %VARitemC + %VARitemD")

I guess I probably need to put the total into a variable as well to evaluate it??
This may help:

%_total=%calc("%VARitemA + %VARitemB + %VARitemC + %VARitemD + 0.001")%-

total: %SUBPATT(1)
That's great, thank you so much Marck!  Works like a charm!

What is %SUBPATT(1) supposed to do?  It didn't make a difference if I left it out.

So here's my final code.. it *seems* to work ok, please let me know if you see any problems before I start using it with real customers!

The only thing is, if I enter $10, it doesn't append the ".00" to the end of it.  Is there an easy fix for that?

the code:
%VARitemA=%InputBox('Price of item #1 (C$):') %-
%VARitemB=%InputBox('Price of item #2 (C$):') %-
%VARitemC=%InputBox('Price of item #3 (C$):') %-
%VARshipping=%InputBox('SHIPPING cost (C$):') %-
%VARexchange=%InputBox('EXCHANGE rate (2 dec places): ') %-
%_subtotal1=%calc("%VARitemA + %VARitemB + %VARitemC + 0.001")%-
%_subtotal2=%calc("%_subtotal1 + %VARshipping")%-
%_total=%calc("%_subtotal2 / %VARexchange")%-
Item subtotal: $%REGEXPMATCH="%_subtotal1"
Subtotal (with shipping): $%REGEXPMATCH="%_subtotal2"
Grand total after exchange (%VARexchange%Chr(37)): $%REGEXPMATCH="%_total"
Okay. Here's where it gets interesting.

moz zer wrote:
The only thing is, if I enter $10, it doesn't append the ".00" to the end of it. Is there an easy fix for that?

That's what "+ 0.001" is for. It adds decimal places. The regex then trims it to the first two.

What we now should do is put the "2 decimals" logic into a callable QT - call it dec2 for example.

%REGEXPMATCH='%Calc("%_1 + 0.001")'%-

then you call the function like this:


because you'll need to perform the procedure on each of the totaled figures.

Subpatt(1) selects and uses the 1st match result (in this case the only result).

So, your code looks like this:

%VARitemA=%InputBox('Price of item #1 (C$):') %-
%VARitemB=%InputBox('Price of item #2 (C$):') %-
%VARitemC=%InputBox('Price of item #3 (C$):') %-
%VARshipping=%InputBox('SHIPPING cost (C$):') %-
%VARexchange=%InputBox('EXCHANGE rate (2 dec places): ') %-
%_subtotal1=%calc("%VARitemA + %VARitemB + %VARitemC")%-
%_subtotal2=%calc("%_subtotal1 + %VARshipping")%-
%_total=%calc("%_subtotal2 / %VARexchange")%-

Item subtotal: $%qinclude(dec2,%_subtotal1)
Subtotal (with shipping): $%qinclude(dec2,%_subtotal2)
Grand total after exchange (%VARexchange%Chr(37)): $%qinclude(dec2,%_total)

If you wanted to justify the decimal place, you can modify dec2 to align the dots - here's an example for 4 digits ... amounts up to 9999

%REGEXPMATCH='    %Calc("%_1 + 0.001")'%-
yikes.. getting complicated now.  ;)  I'll take a look at this and see what I can learn.  Thanks very much for your help on this!

One other thing I meant to mention... when I show the calculated exchange rate for the item subtotal and then the shipping amount, it rounds the value down.  So if it was actually $33.057 for example, it would display $33.05.  But then when I total everything (with exchange rate), the value is correct.. although it appears to be 1 or 2 cents more than if you simply added the two calculated amounts.  I'm betting I'll have some frugal customers catching this.. if fixing it is much more complicated, I'll just leave it as it is.

Thanks so much for all this..
Ok, that didn't take long to understand!  Only seemed complicated at first glance.. that's great that you can call other templates!  That'll save a lot of wasted space when I end up having 6 or 7 templates for each country.. this way, I just call a QT like 'usa3' if I have 3 items to ship to the States.
Yep, works great!  Well, except for when I try the decimal justification method.. amounts under 100.00 get wiped out (nothing is displayed).  Though, $ 100.00 justifies nicely with $1000.00.
Okay - only thing I'll say is change the .001 to .005 if you want the dec2 QT to round instead of truncate ;-)


You're the best Marck, thanks very much for your help..  
Pages: 1