-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathAddCommas.ahk
69 lines (65 loc) · 1.61 KB
/
AddCommas.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
;********************
;* *
;* Add Commas *
;* *
;********************
;
;
; Description
; ===========
; This function adds the appropriate comma characters "," to a number for
; display purposes. For example, "12345" is converted as "12,345".
;
; Original code by Laszlo, enhanced by PhiLho. Original forum topic:
;
; http://www.autohotkey.com/forum/viewtopic.php?t=12754
;
;
;
; Return Value
; ============
; Formatted number (string)
;
;
;
; Calls To Other Functions
; ========================
; (None)
;
;
;-------------------------------------------------------------------------------
AddCommas(p_Number) {
;[==============]
;[ Initialize ]
;[==============]
p_Number=%p_Number% ;-- AutoTrim
if SubStr(p_Number,1,1)="-"
{
l_Sign:="-"
StringTrimLeft p_Number,p_Number,1
}
;[========================]
;[ Add commas as needed ]
;[========================]
loop parse,p_Number,.
{
if A_Index=1
{
l_IntLen:=StrLen(A_LoopField)
loop parse,A_LoopField
{
if (mod(l_IntLen-A_Index,3)=0 and A_Index<>l_IntLen)
l_Number:=l_Number . A_LoopField . ","
else
l_Number:=l_Number . A_LoopField
}
}
else
l_Number:=l_Number . "." . A_LoopField
}
;[==========================]
;[ Return converted value ]
;[==========================]
l_Return:=l_Sign . l_Number
return l_Return
}