d4rk Posted May 30, 2008 Share Posted May 30, 2008 (edited) Hi, i have searched a while on the forum and in the help file but i can't see some thing help to make a number better behavior (or at least easier to read with me ) , so i made this UDFs to : Get a better number performance (999.999 instead of 999999) & (999.999.1234 instead of 999999.1234) You can mofidy the seperater (.), here is the it Update : Now work on both Int & float _NumberFormat() $InputNumber : Number or Variable to convert $Leader : Lead character , ie: $ $Seperater : Seperater $InputCounter : Number of "0" to stand after the number expandcollapse popup;||----------------------------------------------------------|| ;||----------------------------------------------------------|| ;||Function _NumberFormat() ;||Syntax: _NumberFormat($InputNumber,$Leader,$Seperater,$InputCounter) ;||Requirement(s): <Array.au3> ;||On Success : Return converted number ;||On Fail : Exit script ;||Author : d4rk < Le Khuong Duy > ;||----------------------------------------------------------|| ;||----------------------------------------------------------|| #include <Array.au3> Func _NumberFormat($InputNumber,$Leader="",$Seperater=".",$InputCounter=3) $RawNumber=String($InputNumber) If (Not IsNumber($InputCounter)) or (Number($InputCounter)>9) Then ConsoleWriteError("Call with wrong $InputCounter ") Exit EndIf Dim $Counter For $i=1 to $InputCounter $Counter= $Counter & "0" Next Select Case StringIsInt($RawNumber) $Array=StringSplit(number($RawNumber),"") _ArrayDelete($Array,0) For $i=UBound($Array) to 0 step -3 _ArrayInsert($Array,$i,$Seperater) $Number=_ArrayToString($Array,"") if StringLeft($Number,1)=$Seperater then $Number=StringTrimLeft($Number,1) EndIf if StringRight($Number,1)=$Seperater then $Number=StringTrimRight($Number,1) EndIf Next Return $Leader & $Number & $Counter ;--------------------------------------------- Case StringIsFloat($RawNumber) $NumString=String($RawNumber) $Pos=StringInStr($NumString,".",2) if $Pos Then $Cut=StringMid($NumString,$Pos) $DecimalPart=StringReplace($Cut,".",$Seperater) $MainPart=StringTrimRight($NumString,StringLen($NumString)-$Pos+1) EndIf $Array=StringSplit(number($MainPart),"") _ArrayDelete($Array,0) For $i=UBound($Array) to 0 step -3 _ArrayInsert($Array,$i,$Seperater) $Number=_ArrayToString($Array,"") if StringLeft($Number,1)=$Seperater then $Number=StringTrimLeft($Number,1) EndIf if StringRight($Number,1)=$Seperater then $Number=StringTrimRight($Number,1) EndIf Next Return $Leader & $Number & $DecimalPart & $Counter ;--------------------------------------------- Case Else ConsoleWriteError("Can not perform a convert since the variable contain string ") Exit EndSelect EndFuncoÝ÷ ØLZ^jëh×6#include <NumberFormat.au3> MsgBox(64,"",_NumberFormat(999999,"",","));=> 999,999 MsgBox(64,"",_NumberFormat(984.5678,"",","));=> 984,5678 MsgBox(64,"",_NumberFormat(984,"$"));=> $984 MsgBox(64,"",_NumberFormat(224000,"$",",",0));=> $224,000 Edited June 7, 2008 by d4rk [quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys Link to comment Share on other sites More sharing options...
ptrex Posted May 30, 2008 Share Posted May 30, 2008 @d4rk Usefull, but the name of the UDF would be better "_NumberFormat" regards, ptrex Contributions :Firewall Log Analyzer for XP - Creating COM objects without a need of DLL's - UPnP support in AU3Crystal Reports Viewer - PDFCreator in AutoIT - Duplicate File FinderSQLite3 Database functionality - USB Monitoring - Reading Excel using SQLRun Au3 as a Windows Service - File Monitor - Embedded Flash PlayerDynamic Functions - Control Panel Applets - Digital Signing Code - Excel Grid In AutoIT - Constants for Special Folders in WindowsRead data from Any Windows Edit Control - SOAP and Web Services in AutoIT - Barcode Printing Using PS - AU3 on LightTD WebserverMS LogParser SQL Engine in AutoIT - ImageMagick Image Processing - Converter @ Dec - Hex - Bin -Email Address Encoder - MSI Editor - SNMP - MIB ProtocolFinancial Functions UDF - Set ACL Permissions - Syntax HighLighter for AU3ADOR.RecordSet approach - Real OCR - HTTP Disk - PDF Reader Personal Worldclock - MS Indexing Engine - Printing ControlsGuiListView - Navigation (break the 4000 Limit barrier) - Registration Free COM DLL Distribution - Update - WinRM SMART Analysis - COM Object Browser - Excel PivotTable Object - VLC Media Player - Windows LogOnOff Gui -Extract Data from Outlook to Word & Excel - Analyze Event ID 4226 - DotNet Compiler Wrapper - Powershell_COM - New Link to comment Share on other sites More sharing options...
d4rk Posted May 30, 2008 Author Share Posted May 30, 2008 ok, i'll rename it thanks so much for reply [quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys Link to comment Share on other sites More sharing options...
ResNullius Posted May 30, 2008 Share Posted May 30, 2008 (edited) Hi, i have searched a while on the forum and in the help file but i can't see some thing help to make a number better behavior (or at least easier to read with me ) , so i made this UDFs to : Get a better number performance (999.999 instead of 999999) You can mofidy the seperater (.), here is my little UDFs, hope to see some replys ( unlike my poor TP)Seems to return nothing if you have a decimal configuration. ie: $oldnum=100 + 900.50 Here's something similar that I wrote a while back to deal with a specific formatting challenge I had. Also allows you to specify a leading character like "$" I was trying to emulate the string picture formating function available in foxpro. Not finished, but does the basics. expandcollapse popup;example $oldnum=100 + 900.50 $Price = _StringFormatPict($oldnum,"$ ",",") MsgBox(64,"",$price) #include-once ;============================================================================== ; ToDo: Decimal test, search for & strip AlphaChars, ; syntax like @J$ 999.99 ; ; Function Name: _StringFormatPict() ; Description: Formats a numeric string or number to a given picture using ; user specified precision, width, thousands separators, and ; prefixes ($ sign, etc) ; ; Syntax: _StringFormatPict($string, $prefix, $kSep) ; ; Parameter(s): $string = string or number to format ; $prefix = leading character to insert ; $kSep = thousands separartor ; (ie: "," for 1,000.00 vs. 1000.00) ; ; Requirement(s): None ; ; Return Value(s): On Success - Returns the string formatted as specified ; On Failure - -1 and sets @ERROR = 1 ; ; Author(s): ResNullius ; ;=============================================================================== ; Func _stringFormatPict($string, $prefix, $kSep) local $pict = "" $string = String(StringFormat("%.2f",$String)) $pict = StringSplit($string,".") $int = $pict[1] $count=0 If StringLen($int) = 3 OR (StringLen($int) = 4 AND StringLeft($int,1) == "-" ) then $int = $int Else For $i = 1 to StringFormat("%d",(StringLen($pict[1])-1)/3) $int = StringReplace($int,StringRight($int,3*$i+$count),$kSep & StringRight($int,3*$i+$count)) $count += 1 Next EndIf $int = $prefix & $int $pict = $int & "." & $pict[2] return $pict Endfunc Edit: Not meant to hijack your thread with my own example, just thought maybe you could use some ideas from it. Edited May 30, 2008 by ResNullius Link to comment Share on other sites More sharing options...
d4rk Posted May 31, 2008 Author Share Posted May 31, 2008 Seems to return nothing if you have a decimal configuration yes, acctually i was going to do this ... and here it is, updated [quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys Link to comment Share on other sites More sharing options...
litlmike Posted June 6, 2008 Share Posted June 6, 2008 (edited) I am sooooo glad you made this. I was just looking for a way to do this when I saw your UDF. I am kinda suprised this has not already been done by someone else. Good luck completing it, I hope it makes it into the official version. *EDIT:* P.S. if you could include a way to retrun number that includes a currency symobl, like $, that would be great. Edited June 6, 2008 by litlmike _ArrayPermute()_ArrayUnique()Excel.au3 UDF Link to comment Share on other sites More sharing options...
ResNullius Posted June 7, 2008 Share Posted June 7, 2008 *EDIT:* P.S. if you could include a way to retrun number that includes a currency symobl, like $, that would be great.See my take on this above for a way to do it it with $ or any defined prefix. Link to comment Share on other sites More sharing options...
Andreik Posted June 7, 2008 Share Posted June 7, 2008 Nice UDF d4rk. Link to comment Share on other sites More sharing options...
d4rk Posted June 7, 2008 Author Share Posted June 7, 2008 Updated ... as litlmike's request [quote]Don't expect for a perfect life ... Expect a least troubles ones[/quote]Contact me : ass@kiss.toWhat I Have Done :Favorites Manager Mangage your favorite's folder, that's coolPC Waker For those who want to save stickersWebScipts Supporter For those who've just started with Web and WebScriptsTemporary Looker Simple but powerful to manage your Temporary folder, you know what you downloaded[UDF] _NumberFormat() Better performance on number display[UDF] _DirGet() What a folder contain [how many (hidden,normal,...) files], with one line of code[UDF] _IsPressEs() Just like _IsPress() but for a group of keys Link to comment Share on other sites More sharing options...
litlmike Posted June 13, 2008 Share Posted June 13, 2008 Thanks for the update. I also made a change to it, but you could even go further by removing all strings: expandcollapse popup;||----------------------------------------------------------|| ;||----------------------------------------------------------|| ;||Function _NumberFormat() ;||Syntax: _NumberFormat($InputNumber,$Leader,$Seperater,$InputCounter) ;||Requirement(s): <Array.au3> ;||On Success : Return converted number ;||On Fail : Exit script ;||Author : d4rk < Le Khuong Duy > ;||----------------------------------------------------------|| ;||----------------------------------------------------------|| #include <Array.au3> Func _NumberFormat($InputNumber, $Leader = "", $Seperater = ".", $InputCounter = 3) $RawNumber = String($InputNumber) $RawNumber = StringRegExpReplace($RawNumber, "[$,]", "") ;So the Func Fails less often ConsoleWrite("$RawNumber = " & $RawNumber & @CR) If (Not IsNumber($InputCounter)) Or (Number($InputCounter) > 9) Then ConsoleWriteError("Call with wrong $InputCounter ") Exit EndIf Dim $Counter For $i = 1 To $InputCounter $Counter = $Counter & "0" Next Select Case StringIsInt($RawNumber) $Array = StringSplit(Number($RawNumber), "") _ArrayDelete($Array, 0) For $i = UBound($Array) To 0 Step -3 _ArrayInsert($Array, $i, $Seperater) $Number = _ArrayToString($Array, "") If StringLeft($Number, 1) = $Seperater Then $Number = StringTrimLeft($Number, 1) EndIf If StringRight($Number, 1) = $Seperater Then $Number = StringTrimRight($Number, 1) EndIf Next Return $Leader & $Number & $Counter ;--------------------------------------------- Case StringIsFloat($RawNumber) $NumString = String($RawNumber) $Pos = StringInStr($NumString, ".", 2) If $Pos Then $Cut = StringMid($NumString, $Pos) $DecimalPart = StringReplace($Cut, ".", $Seperater) $MainPart = StringTrimRight($NumString, StringLen($NumString) - $Pos + 1) EndIf $Array = StringSplit(Number($MainPart), "") _ArrayDelete($Array, 0) For $i = UBound($Array) To 0 Step -3 _ArrayInsert($Array, $i, $Seperater) $Number = _ArrayToString($Array, "") If StringLeft($Number, 1) = $Seperater Then $Number = StringTrimLeft($Number, 1) EndIf If StringRight($Number, 1) = $Seperater Then $Number = StringTrimRight($Number, 1) EndIf Next Return $Leader & $Number & $DecimalPart & $Counter ;--------------------------------------------- Case Else ConsoleWriteError("Cannot Convert Variable, it Contains a String" & @CRLF & "Shame On You") MsgBox(0, "ERROR!", "Cannot Convert Variable, it Contains a String" & @CRLF & "Shame On You", 5) Exit EndSelect EndFunc ;==>_NumberFormat _ArrayPermute()_ArrayUnique()Excel.au3 UDF Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now