Jump to content

Recommended Posts

Posted (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

;||----------------------------------------------------------||
;||----------------------------------------------------------||
;||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 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

Posted

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

Posted (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.

;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 by ResNullius
Posted

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

Posted (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 by litlmike
Posted

*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.
Posted

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

Posted

Thanks for the update. I also made a change to it, but you could even go further by removing all strings:

;||----------------------------------------------------------||
;||----------------------------------------------------------||
;||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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...