Posts: 12
Threads: 4
Joined: Jun 2016
Yeah so more or less advanced but I struggle with this.
So I get the value of either something like "37.5K" or "134K".
And I want it to look like a normal number, so 37500 or 134000.
The problem is I cant just replace the "K" with "000" because then the
"37.5K" would look like 375000 which is too much.
So I would need something like:
[if there is a "." in the Name then replace "K" with "00", if not replace with "000"]
Is there a way to that in Quick Macros?
Posts: 12,079
Threads: 141
Joined: Dec 2002
Macro
Macro632
str s="{37.5K 134K 21.37K 2.351K 0.542K 0.017K}"
s.replacerx("\.(\d)K\b" "${1}00")
s.replacerx("\.(\d\d)K\b" "${1}0")
s.replacerx("\.(\d\d\d)K\b" "${1}")
s.replacerx("(\d)K\b" "${1}000")
s.replacerx("\b0+([1-9])" "$1")
out s ;;{37500 134000 21370 2351 542 17}
or
Macro
Macro964
str s="{37.5K 134K 21.37K 2.351K 0.542K 0.017K}"
REPLACERX r.frepl=&sub.Callback_str_replacerx
s.replacerx("\b\d+(?:\.\d+)?K\b" r)
out s
#sub Callback_str_replacerx
function# REPLACERXCB&x
x.match=val(x.match 2)*1000
Posts: 12
Threads: 4
Joined: Jun 2016
Thank you very much!
Also sometimes there is no "K" in it at all, so for example "3,148".
I get an error with the second code:
unknown sub-function
This is highlighted in the Macro "sub.Callback_str_replacerx".
edit: It seems like #sub creates a macro inside the macro
How can I continue with the macro after this, the macro is also looped?
edit2: Got it, just had to move the #sub macro to the end of the script.
But the result is the same for numbers like "1,300" or "2,500" it doesnt work
and it puts out only "2" or "3".
Posts: 12,079
Threads: 141
Joined: Dec 2002
Macro
Macro965
str s="{37.5K 134K 21.37K 2.351K 0.542K 0.017K 25 1,300 2,500K}"
REPLACERX r.frepl=&sub.Callback_str_replacerx
s.replacerx("\b\d+(?:,\d+)*(?:\.\d+)?K?\b" r)
out s ;;{37500 134000 21370 2351 542 17 25 1300 2500000}
#sub Callback_str_replacerx
function# REPLACERXCB&x
x.match.findreplace(",")
double d=val(x.match 2)
if(x.match.end("K")) d*1000
x.match=d
Posts: 12
Threads: 4
Joined: Jun 2016
Works like a charm. Thank you!