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 

Question about formating numbers... 1000 to 1,000 HOW?
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
ahknoob



Joined: 13 Mar 2007
Posts: 11

PostPosted: Thu Jun 11, 2009 1:56 am    Post subject: Question about formating numbers... 1000 to 1,000 HOW? Reply with quote

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?
Back to top
View user's profile Send private message Visit poster's website
JDN



Joined: 24 Mar 2004
Posts: 299

PostPosted: Thu Jun 11, 2009 2:00 am    Post subject: Reply with quote

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/viewtopic.php?t=43566&highlight=convert+numbers+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.
Back to top
View user's profile Send private message
JDN



Joined: 24 Mar 2004
Posts: 299

PostPosted: Thu Jun 11, 2009 2:12 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
poo_noo



Joined: 08 Dec 2006
Posts: 248
Location: Sydney Australia

PostPosted: Thu Jun 11, 2009 3:34 am    Post subject: Reply with quote

I found this in the AHK forum, Use the word 'formatnumber" as a search string

http://www.autohotkey.com/forum/viewtopic.php?t=15218&highlight=formatnumber
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
Back to top
View user's profile Send private message Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Thu Jun 11, 2009 5:41 am    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Send e-mail
animeaime



Joined: 04 Nov 2008
Posts: 1045

PostPosted: Thu Jun 11, 2009 6:42 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Send e-mail
ahknoob



Joined: 13 Mar 2007
Posts: 11

PostPosted: Fri Jun 12, 2009 7:46 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
ahknoob



Joined: 13 Mar 2007
Posts: 11

PostPosted: Fri Jun 12, 2009 7:51 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
berban_



Joined: 16 Mar 2011
Posts: 150
Location: Worcester, Massachusetts

PostPosted: Sat Oct 08, 2011 1:25 am    Post subject: Reply with quote

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 ★★★
Back to top
View user's profile Send private message Visit poster's website
jpjazzy



Joined: 16 Feb 2010
Posts: 799
Location: SciTE

PostPosted: Sat Oct 08, 2011 1:35 am    Post subject: Reply with quote

Did you see when the last reply was posted? Many new solutions have been made for this task...



Why bring up skeletons?
Back to top
View user's profile Send private message Visit poster's website
RedCairo
Guest





PostPosted: Fri Oct 28, 2011 7:43 am    Post subject: Reply with quote

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
Back to top
Gogo
Guest





PostPosted: Fri Oct 28, 2011 8:17 am    Post subject: Reply with quote

There is an error. Try it with 12345678.
Code:
var = 12345678
MsgBox % RegExReplace(var, "(\G|[^\d.]*)\d{1,3}(?=(\d{3})+(\D|$))", "$0,")
Back to top
Odlanir



Joined: 07 Aug 2011
Posts: 649

PostPosted: Fri Oct 28, 2011 10:39 am    Post subject: Reply with quote

Still an error on decimal part. Try
Code:
var = 1234567.34567

_________________
Win7 - Firefox 8.0.1 - AHK_L 1.1.05.06
Please bear with me and my English which is so bad at times that even I don't understand myself
Back to top
View user's profile Send private message
Gogo
Guest





PostPosted: Fri Oct 28, 2011 11:12 am    Post subject: Reply with quote

There is an obsolete asterisk
Code:
var =  1234567.34567
MsgBox % RegExReplace(var, "(\G|[^\d.])\d{1,3}(?=(\d{3})+(\D|$))", "$0,")
Back to top
Odlanir



Joined: 07 Aug 2011
Posts: 649

PostPosted: Fri Oct 28, 2011 4:18 pm    Post subject: Reply with quote

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 8.0.1 - AHK_L 1.1.05.06
Please bear with me and my English which is so bad at times that even I don't understand myself
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
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