Sign in to follow this  
Followers 0
GEOSoft

Number funcs

7 posts in this topic

#1 ·  Posted (edited)

I know that someone else wrote one of these but for the life of me, I don't remember who. I also couldn't turn it up in a search. Gust send your number to _Number_AddSep() and it will return The number with the separators in place.

_Number_ReplaceSep() can be used to change the separator format of a previously separated number. That solves the issue that I do remember, regarding European numbering systems. It can also be used to remove separators.

TIP: If you have a number which is a float, and you want it in the European system, then I suggest that you run _Number_Addseparator() using the defaults (British system) and then convert it with _Number_ReplaceSep(). Otherwise there is the potential for a mal-formatted number being returned which could be difficult to convert later. That's not to say that it couldn't be done though.

These functions are intended to be light weight so don't try to complicate it.

Enjoy.

;===============================================================================
; 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.  Default is comma (",")
;                          $dSep - The separator to use for the decimal value. Default is dot (".")
; Requirement(s):   
; Return Value(s):   Separated number
; Author(s):   George (GEOSoft) Gedye
;===============================================================================

Func _Number_AddSep($iVal, $sSep = ",", $dSep = ".")
   If (NOT StringIsInt($iVal)) AND (NOT StringIsFloat($iVal)) Then Return SetError(1); Integer or float required
   Local $dVal = ""
   If StringInStr($iVal, ".") Then
      $dVal = StringMid($iVal, StringInStr($iVal, "."))
      $iVal = StringLeft($iVal, StringInStr($iVal, ".") -1)
      $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, 1) = $sSep Then $oVal = StringTrimLeft($oVal, 1)
   Return $oVal & $dVal
EndFunc ;<==> _Number_AddSep()

;===============================================================================
; 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()
Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites



Hi,

to answer your first question, here is the discussion: Link

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Hi,

to answer your first question, here is the discussion: Link

Mega

You were close but the thread that I was thinking of is the origional _StringAddComma.

I also just noticed that it made it's way into string.au3 as _StringAddThousandsSep()

hade I noticed that earlier I could have avoided writing some code.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@GEOSoft

Could you not omit the second function by adding just one parameter in the first function.

ConsoleWrite(_Number_AddSep(1000000.52,"EU") & @LF)

;===============================================================================
; 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.  Default is comma (",")
;                           $dSep - The separator to use for the decimal value. Default is dot (".")
; Requirement(s):   
; Return Value(s):   Separated number
; Author(s):   George (GEOSoft) Gedye
;===============================================================================

Func _Number_AddSep($iVal, $International = "US")
    If  $International = "US" then 
        $sSep = ","
        $dSep = "."
    Else
        $sSep = "."
        $dSep = ","
    EndIf
    
   If (NOT StringIsInt($iVal)) AND (NOT StringIsFloat($iVal)) Then Return SetError(1); Integer or float required
   Local $dVal = ""
   If StringInStr($iVal, ".") Then
      $dVal = StringMid($iVal, StringInStr($iVal, "."))
      $iVal = StringLeft($iVal, StringInStr($iVal, ".") -1)
      $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, 1) = $sSep Then $oVal = StringTrimLeft($oVal, 1)
   Return $oVal & $dVal
EndFunc ;<==> _Number_AddSep()

Or am I wrong.

regards

ptrex

Edited by ptrex

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@GEOSoft

Could you not omit the second function by adding just one parameter in the first function.

ConsoleWrite(_Number_AddSep(1000000.52,"EU") & @LF)

;===============================================================================
; 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.  Default is comma (",")
;                           $dSep - The separator to use for the decimal value. Default is dot (".")
; Requirement(s):   
; Return Value(s):   Separated number
; Author(s):   George (GEOSoft) Gedye
;===============================================================================

Func _Number_AddSep($iVal, $International = "US")
    If  $International = "US" then 
        $sSep = ","
        $dSep = "."
    Else
        $sSep = "."
        $dSep = ","
    EndIf
    
   If (NOT StringIsInt($iVal)) AND (NOT StringIsFloat($iVal)) Then Return SetError(1); Integer or float required
   Local $dVal = ""
   If StringInStr($iVal, ".") Then
      $dVal = StringMid($iVal, StringInStr($iVal, "."))
      $iVal = StringLeft($iVal, StringInStr($iVal, ".") -1)
      $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, 1) = $sSep Then $oVal = StringTrimLeft($oVal, 1)
   Return $oVal & $dVal
EndFunc ;<==> _Number_AddSep()

Or am I wrong.

regards

ptrex

It could be done that way. The way I have it now allows for people using spaces and whatever else they want and also will allow removal of separators entirely. You would have to read the thread in Chat to see what I mean. I'll play with your version a bit today.

Thanks for the input.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Nice work George! I got your PM only I didn't have time to try them out for you :D English lessons and all.

NP. I was closing off the project I used them in, so I thought I would just post them for others to play with.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0