Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
formatting number with commas
#1
is the an easy way to put a character into a number to mark the thousands, millions etc?

285798181
to this
285,798,181
or this if not in the US
285.798.181 :wink:
An old blog on QM coding and automation.

The Macro Hook
#2
member function str.Format100
Code:
Copy      Help
function [VARIANT'numberstring] [$delimiter];;1245657864 "."

;Formats numberstring 1245657864 to 1.245.657.864
;Default: itself.
;delimiter default is '.'
;!!! NOT implemented: check for double !!!
;EXAMPLES
;str s
;s.Format100("1245657864")


;s=1245657864
;s.Format100


;s.Format100(252545 ",")
;out s


if(!numberstring) numberstring=this;else this=numberstring
if(!delimiter) delimiter="."
ARRAY(int) h.create(3)
h[0] = 9
h[1] = 6
h[2] = 3

if this.len>=10
,this.insert(delimiter this.len-h[0])
,this.insert(delimiter this.len-h[1])
,this.insert(delimiter this.len-h[2])
else if this.len>=7
,this.insert(delimiter this.len-h[1])
,this.insert(delimiter this.len-h[2])
else if this.len>=4
,this.insert(delimiter this.len-h[2])

double needs to be implemented.
pi
#3
HA!
it's a beautiful thing; thanks!

I'll watch out for the double.
An old blog on QM coding and automation.

The Macro Hook
#4
This also works for double.
Code:
Copy      Help
str s="-1234567.1234567"
strrev s
s.replacerx("(\d{3})(?=\d+($|[^\.\w]))" "$1,")
strrev s
out s

There should be an API function for this, but I could not find.
#5
"strrev" is that new?
it doesn't show up coloured for me.
An old blog on QM coding and automation.

The Macro Hook
#6
For QM versions before 2.2.0, declare it:

dll msvcrt [_strrev]$strrev $string
#7
odd.

i've got 2.2.0.5.
An old blog on QM coding and automation.

The Macro Hook
#8
It will become coloured after you run the code and reopen the macro or refresh colors.

In QM 2.2.0, everything that is in WINAPI don't have to be declared in macros.
#9
yep...there it is.

man, RX kicks my can every time!

do you know of a good book on RX?

PS: thanks! i'll have to rember the strrev thing; could come in handy somewhere else too.
An old blog on QM coding and automation.

The Macro Hook
#10
Member function: str.AddCommasToNumbers

Code:
Copy      Help
function$
;Adds commas to large numbers.  
;Note: the number must become a string before sending to this funcion.


;str s="1234567.1234567"
;s.AddCommasToNumbers
;out s



strrev this
this.replacerx("(\d{3})(?=\d+($|[^\.\w]))" "$1,")
strrev this
An old blog on QM coding and automation.

The Macro Hook


Forum Jump:


Users browsing this thread: 3 Guest(s)