AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Comma separated numbers

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
quatermass



Joined: 14 Dec 2005
Posts: 136

PostPosted: Fri Sep 19, 2008 10:04 am    Post subject: Comma separated numbers Reply with quote

Here is a function that takes a large number and pretties it up with commas.

Oddly this ability is not in AHK by default and when I'd program in BBC BASIC I got use to having it on tap.


Code:
Prettynumber(n) {
   RegRead,sThousands,HKEY_CURRENT_USER,Control Panel\International,sThousand
   StringSplit, n, n, .
   If (d := Mod(l := StrLen(Abs(n1)), 3)) and l > 3
      n1 := RegExReplace(n1, "(\d{" . d . "})", "$1" . sThousands, "", 1)
   Return, RegExReplace(n1, "(\d{3})(?!$)", "$1" . sThousands) . (n2 != "" ? "." . n2 : "")
}



So
Code:
number := 123456789
pretty := Prettynumber(number)
msgbox, %pretty%


Gives you 123,456,789

Much easier to understand.

Oh and it should return the correct digit separator symbol.
e.g. 123.456.789 in those countries that use the dot instead of a comma.

Original function 'number()' taken and adapted from the SYSMON Script by MsgBox.
So full credit to him for creating it!


Share and enjoy....
_________________
Stuart Halliday
Back to top
View user's profile Send private message
wheelwright
Guest





PostPosted: Fri Sep 19, 2008 11:19 am    Post subject: Re: Comma separated numbers Reply with quote

quatermass wrote:
Here is a function that takes a large number and pretties it up with commas.

Oddly this ability is not in AHK by default and when I'd program in BBC BASIC I got use to having it on tap.

Oh and it should return the correct digit separator symbol.
e.g. 123.456.789 in those countries that use the dot instead of a comma.

The wheel reinvented, again and again. Shocked See this post for several solutions to the problem. The regex solutions allow the user to set the delimiter, while SKAN's post allows the user to specify any Locale identifier supported by windows.
SKAN's solution uses the system function GetNumberFormat, and its first parameter is a Local identifier constant.
GetNumberFormat info is at: http://msdn.microsoft.com/en-us/library/ms776317%28VS.85%29.aspx
Local identifier constant info is at: http://msdn.microsoft.com/en-us/library/ms776260%28VS.85%29.aspx
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group