 Number.au3
   
			Number.au3
			
			Last modified:    Wednesday, September 30, 2020
			
			
;*******************************************************************************
;
;   Function List
;         _Number_AddSep()
;         _Number_FormatAsCurrency()
;         _Number_ReplaceSep()
;
;*******************************************************************************
#include-once
;===============================================================================
; Function Name:   _Number_AddSep()
; Description:   Add separators to a number
; Syntax:   _Number_AddSep("Number" [, "separator"])
; Parameter(s):   $iVal - The number to add separators to
;                           $sSep - The separator to use.
;                           $dSep - The separator to use for the decimal value.
; Requirement(s):   
; Return Value(s):   Separated number
; Author(s):   George (GEOSoft) Gedye
; Note(s):   $iVal MUST be given either as an integer or a float
;            The defaults for $sSep and $dSep are read from the registry
;===============================================================================
Func _Number_AddSep($iVal, $sSep = -1, $dSep = -1)
  If (NOT StringIsInt($iVal)) AND (NOT StringIsFloat($iVal)) Then Return SetError(1)
  If $sSep = "" Or StringRegExp($sSep, "(?i)-1|default") Then _
      $sSep = RegRead("HKCU\Control Panel\International", "sThousand") 
  If $dSep = ""  Or StringRegExp($dSep, "(?i)-1|default") Then _
      $dSep = RegRead("HKCU\Control Panel\International", "sDecimal") 
  Local $dVal = ""
  If StringIsFloat($iVal) Then
    $dVal = StringMid($iVal, StringInStr($iVal, "."))
    $iVal = Int($iVal)
    $dVal = StringReplace($dVal, ".", $dSep)
  EndIf
  Local $oVal = ""
  $cArray = StringSplit($iVal,"")
  For $I = Ubound($cArray)-3 to 1 step -3
    $cArray[$I] = $sSep & $cArray[$I]
  Next
  For $I = 1 To Ubound($cArray)-1
    $oVal &= $cArray[$I]
  Next
  If StringLeft($oVal, StringLen($sSep)) = $sSep Then $oVal = _
      StringReplace($oVal, $sSep, "", 1)
  Return $oVal & $dVal
EndFunc   ;<==> _Number_AddSep()
;===============================================================================
; Function Name:     _Number_FormatAsCurrency()
; Description:   Return the currency value of a number
; Syntax:       _Number_FormatCurrency($sVal)
; Parameter(s):   $sVal - The number to format
;                 $aSep - if not 0 then it will add thousands separators to the string
; Requirement(s):   
; Return Value(s):   Success - Regional formatted currency value
;                    Failure - Sets @Error to 1 if $sVal is not an integer or a float
; Author(s):   George (GEOSoft) Gedye
; Modification(s):   
; Note(s):    $sVal MUST be given either as an integer or a float
; Example(s):   MsgBox(0, "TEST", _Number_FormatAsCurrency(1844567289.49, 1))
;===============================================================================
Func _Number_FormatAsCurrency($sVal, $aSep = 0)
  If (NOT StringIsInt($sVal)) AND (NOT StringIsFloat($sVal)) Then Return SetError(1)
  If NOT StringInStr($sVal, ".") Then $sVal &= ".00"
  Local $fKey = "HKCU\Control Panel\International", $dVal, $nVal, $dSep, $cSym
  $cSym = RegRead($fKey, "sCurrency"), $nSep = RegRead($fKey, "sThousand")
  $dSep = RegRead($fKey, "sDecimal")
  $nVal = StringRegExp($sVal, "(\d*)(\" & $dSep & ")(\d{0,2})(\d*)", 1)
  $nVal[0] = StringFormat("%d", $nVal[0])
  If $aSep <> 0 Then $nVal[0] = _Number_AddSep($nVal[0], $nSep)
  Return StringFormat($cSym & "%s" & $dSep & "%02d",$nVal[0],$nVal[2])
EndFunc   ;<==> _Number_FormatAsCurrency()
;===============================================================================
; Function Name:   _Number_ReplaceSep()
; Description:   Replace the separators in a number (convert to/from European style)
; Syntax:   _Number_ReplaceSep("number")
; Parameter(s):   $iNum - Number  to change
;                           $nSep - Existing number separator
;                           $dSep - Existing decimal indicator
; Requirement(s):   
; Return Value(s):   
; Author(s):   GEOSoft
;===============================================================================
Func _Number_ReplaceSep($iNum, $nSep = ",", $dSep = ".")
  If StringInStr($iNum, $dSep) Then $iNum = StringReplace($iNum, $dSep, "~")
  $iNum = StringReplace($iNum, $nSep, $dSep)
  $iNum = StringReplace($iNum, "~", $nSep)
  Return $iNum
EndFunc   ;<==> _Number_ReplaceSep() 
         