Jump to content

StrAddComma


SmOke_N
 Share

Recommended Posts

  • Moderators

Derived from Gary bringing up an old post on _Addcomma() and saying it could probably be done shorter now.

(Not thoroughly tested)

;==================================================================================================
; Function Name:        _StrAddComma($sStr, $sSeperator, $sEnd)
;
; Parameters:           $sStr:        The Numbered string to pass
;                       $sSeperator:  The seperator string you wish to use as the delimeter
;                                       Default or -1:  ','
;                       $sEnd:        The End delimeter to distinguish decimal remainder
;                                       Default or -1:  '.'
;
; Description:          Adds your preferred delimeter to a Number string
;
; Requirement(s)        AuotIt Beta 3.2.1.8 or higher
;
; Return Value(s)       On Success:    @Error: = 0 No Error | Returns the number string with its sperators
;                       On Failure:    @Error = 1: String passed was not a number
;
; Author(s):            SmOke_N
;==================================================================================================
Func _StrAddComma($sStr, $sSeperator = -1, $sEnd = -1)
    If $sSeperator = -1 Or $sSeperator = Default Then $sSeperator = ','
    If $sEnd = -1 Or $sEnd = Default Then $sEnd = '.'
    Local $aNum = StringSplit($sStr, $sEnd), $nHold, $aSRE, $fUB, $iAdd
    If UBound($aNum) > 2 Then
        $aSRE = StringRegExp($aNum[1], '(\d+)(\d{3})', 3)
        $fUB = True
    Else
        $aSRE = StringRegExp($sStr, '(\d+)(\d{3})', 3)
    EndIf
    If UBound($aSRE) = 2 Then
        While IsArray($aSRE)
            $nHold = $sSeperator & $aSRE[1] & $nHold
            $aSRE = StringRegExp($aSRE[0], '(\d+)(\d{3})', 3)
            $iAdd += 1
        WEnd
    EndIf
    If $fUB And $nHold Then
        Return SetError(0, 0, StringTrimRight($sStr, ($iAdd + 1) * 3) & $nHold & $sEnd & $aNum[2])
    ElseIf $nHold Then
        Return SetError(0, 0, StringTrimRight($sStr, $iAdd * 3) & $nHold)
    EndIf
    Return SetError(1, 0, $sStr)
EndFunc

Damn Edits got so big, gave them a codebox :whistle:

CODE

Edit:

Fixed Return Error

Edit2:

Had to remove StringIsFloat/StringIsInt arguement.

Edit3:

Shortened to 21 lines.

Edit4:

Fixed a bug that xcal noticed... Thanks

Edit5:

Last fix that I did xcal noticed, broke the decimal return, that's fixed now.

Edit6:

Edited for submission.

Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

  • Moderators

Nice. I like it.

.. now, if you can write this for me in C++ :whistle:. lol

Should be easier and smaller.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
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
 Share

  • Recently Browsing   0 members

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