Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert digit grouping notation
#1
I have written the following function to insert a digit grouping comma to a double precision value converted to string:

Function tempf11
Code:
Copy      Help
double x=12345.68
str s=x
int i=find(s ".")

i=iif(i<0 len(s)-3 i-3)
if i > 0; s.insert("," i)

out s

I wonder whether there exists a system function to do it in an elegant way.

Many thanks in advance.
#2
Exists, but I don't like it.

Macro Macro2482
Code:
Copy      Help
str s="12345678.754" ;;result: 12,345,678.75
;str s="-12345678.754" ;;result: (12,345,678.75). Why?

NUMBERFMT n.Grouping=3
n.lpDecimalSep="."
n.lpThousandSep=","
n.NumDigits=2

str ss.all(s.len*2)
ss.fix(GetNumberFormat(LOCALE_USER_DEFAULT 0 s &n ss ss.nc))
;if(!ss.len) ss=s

out ss
#3
My version:
Macro Macro2485
Code:
Copy      Help
str s="-3456789.1234"

strrev s
REPLACERX r.ifrom=findc(s '.')+1
r.repl="$0,"
s.replacerx("\d{3}(?=\d)" r)
strrev s

out s
#4
Many thanks and Regards.


Forum Jump:


Users browsing this thread: 1 Guest(s)