AutoHotkey Community

It is currently May 26th, 2012, 6:32 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: June 11th, 2009, 2:56 am 
Offline

Joined: March 13th, 2007, 10:54 pm
Posts: 11
As the title says i'm trying to format numbers to places.

Format numbers such as;

1000 to 1,000
10000 to 10,000
900000 to 900,000

How is this possible? Or perhaps just a direction?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 3:00 am 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
Sad to say, I don't know of any built-in function that does this.

Do you use any other programming languages like C++ or Delphi?

You might consider doing it in that language and then bringing the formatted version back into AHK.

But I bet someone has already done this in AHK.

If I were you, I would spend a few minutes searching the AHK forums and then using Google to search the net.

Otherwise, the worst case scenario is that you convert the number to a string and then remove the minus sign temporarily if app.

Then loop from right to left in the string and place the commas into the string your self.

It's ugly but it's the last straw.

I bet someone has already done this though.

There are some examples in this thread:

[url]
http://www.autohotkey.com/forum/viewtop ... rs+strings
[/url]

Unfortunately, it's not exactly what you want.

Can you create the formatted string using an external EXE file and then store the result in a file and then use AHK to read the file to get the result? That might be possible if the number of conversions is not too large. Most languages have many facilities to do this quite easily.

For example, if you can use Delphi, it has many format functions such as,

Format function
FormatBuf function
FmtStr procedure
StrFmt function
StrLFmt function

I'm pretty sure that many C functions library have similar functions.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 3:12 am 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
Here is a Delphi function that I once wrote to insert separators into a character sting containing a number. It is well tested and works OK. If you can't find a more direct AHK function, it shouldn't be too difficult to convert this code from Delphi to AHK. Good Luck!

Code:
{
Insert_Sep - convert integer to a string with separators
I do this because the %n format string does not work correctly on numbers with more than 18 digits.
See the defective version of this function above.
}
function Insert_Sep(A:int64; Separator:char):string;
var S:string; Negative:boolean;
begin
   Result:= '';
   try S:= IntToStr(A); except on EConvertError do exit end;
   if Length(S) = 0 then exit;
   Negative:= S[1] = '-';                                             // negative number
   if Negative then Delete(S, 1, 1);
   while Length(S) > 0 do begin
     if Length(S) >= 3 then begin
       if Length(Result) = 0 then
          Result:= Copy(S, Length(S) - 2, 3)
       else
          Result:= Copy(S, Length(S) - 2, 3) + Separator + Result;
       Delete(S, Length(S) - 2, 3);
     end
     else begin
       if Length(Result) = 0 then
          Result:= S
       else
          Result:= S + Separator + Result;
       S:= ''
     end;
   end;
   if Negative then Result:= '-' + Result;
end;


In the above code, I would use the comma as the separator. It is passed to the function above as the second parameter above.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 4:34 am 
Offline

Joined: December 8th, 2006, 5:17 am
Posts: 248
Location: Sydney Australia
I found this in the AHK forum, Use the word 'formatnumber" as a search string

http://www.autohotkey.com/forum/viewtop ... rmatnumber
Code:
; function to format the amount field with commas between the 000s
FormatNumber(Amount) {                      ; add commas after blocks of 3 digits left of decimal point (if any)
   StringReplace Amount, Amount, -
   IfEqual ErrorLevel,0, SetEnv Sign,-
   Loop Parse, Amount, .
      If (A_Index = 1) {
         len := StrLen(A_LoopField)
         Loop Parse, A_LoopField
            If (Mod(len-A_Index,3) = 0 and A_Index != len)
                x = %x%%A_LoopField%,
            Else x = %x%%A_LoopField%
         } Else Return Sign x "." A_LoopField
      Return Sign x
   }

_________________
Paul O


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 6:41 am 
Online
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8776
Code:
Num := 123456789
VarSetCapacity( fNum,32 )
DllCall( "GetNumberFormat",UInt,0x0409,UInt,0,Str,Num,UInt,0,Str,fNum,Int,32 )
StringTrimRight, fNum, fNum, 3
MsgBox, % Fnum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 11th, 2009, 7:42 am 
Offline

Joined: November 4th, 2008, 9:23 am
Posts: 1045
Use the below 1000sSeparate function.

Code:
Number = 123456

;outputs 123,456
MsgBox, % 1000sSeparate(Number)

Number = 12345.61234

;outputs 12,345.61234
MsgBox, % 1000sSeparate(Number)

1000sSeparate(Number)
{
    if inStr(Number, ".")
    {
        ;don't format digits after decimal place
        return RegExReplace(Number, "(\d)(?=(?:\d{3})++\.)", "$1,")
    }
    else
        return RegExReplace(Number, "(\d)(?=(?:\d{3})++$)", "$1,")
}

_________________
As always, if you have any further questions, don't hesitate to ask.

Add OOP to your scripts via the Class Library. Check out my scripts.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 12th, 2009, 8:46 pm 
Offline

Joined: March 13th, 2007, 10:54 pm
Posts: 11
THanks a lot you guys! I tried the first few posts suggestions but I didn't get anywhere. Now we're talking! Thanks a lot! Funny thing is... you'd think this would be a lot simpler or perhaps have a built-in feature. But that's ok. The AHK DEVS are amazing how it is.

Again, thanks.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 12th, 2009, 8:51 pm 
Offline

Joined: March 13th, 2007, 10:54 pm
Posts: 11
Awsome job.

SKAN wrote:
Code:
Num := 123456789
VarSetCapacity( fNum,32 )
DllCall( "GetNumberFormat",UInt,0x0409,UInt,0,Str,Num,UInt,0,Str,fNum,Int,32 )
StringTrimRight, fNum, fNum, 3
MsgBox, % Fnum


As for everyone else.. Your still awesome. I prefer the least amount of code possible. My scripts tend to get long. And as far as I'm concerned.. they all format the same way. Just a lot less work.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 8th, 2011, 2:25 am 
Offline

Joined: March 16th, 2011, 6:12 pm
Posts: 172
Location: Worcester, Massachusetts
Why so long? The below RegEx is only one line, can handle decimal points and leading/trailing spaces, and is much faster to boot

Code:
RegExReplace(var, "\G[^\d.]*\d{1,3}(?=\d{3}+(\D|$))", "$0,")

_________________
★★★ Email me at berban at aim full stop com ★★★


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 8th, 2011, 2:35 am 
Offline

Joined: February 16th, 2010, 8:01 am
Posts: 800
Location: SciTE
Did you see when the last reply was posted? Many new solutions have been made for this task...

Image

Why bring up skeletons?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 8:43 am 
berban_ wrote:
Why so long? The below RegEx is only one line, can handle decimal points and leading/trailing spaces, and is much faster to boot

Code:
RegExReplace(var, "\G[^\d.]*\d{1,3}(?=\d{3}+(\D|$))", "$0,")


Thanks, this was very useful to me.

(Skeletons... the point of a forum with search is that it's eternal...)

PJ


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 9:17 am 
There is an error. Try it with 12345678.
Code:
var = 12345678
MsgBox % RegExReplace(var, "(\G|[^\d.]*)\d{1,3}(?=(\d{3})+(\D|$))", "$0,")


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 11:39 am 
Offline

Joined: August 7th, 2011, 1:23 pm
Posts: 754
Still an error on decimal part. Try
Code:
var = 1234567.34567

_________________
Win7 - Firefox 10.0.2 - AHK_L 1.1.07.00
Please bear with me and my English which is so bad at times that even I don't understand myself


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 12:12 pm 
There is an obsolete asterisk
Code:
var =  1234567.34567
MsgBox % RegExReplace(var, "(\G|[^\d.])\d{1,3}(?=(\d{3})+(\D|$))", "$0,")


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2011, 5:18 pm 
Offline

Joined: August 7th, 2011, 1:23 pm
Posts: 754
Nice Gogo! obviously for European style you must invert dot and comma.
Code:
var =  1234567,34567
MsgBox % RegExReplace(var, "(\G|[^\d,])\d{1,3}(?=(\d{3})+(\D|$))", "$0.")

_________________
Win7 - Firefox 10.0.2 - AHK_L 1.1.07.00
Please bear with me and my English which is so bad at times that even I don't understand myself


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: rbrtryn, Retro Gamer, SKAN, Yahoo [Bot] and 52 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group