Jump to content

StringAddThousandsSep only for pos. values?


ReFran
 Share

Recommended Posts

Mmmh,

tried this from the helpfile - only changed to negative values. Gives back positive values.

Known problem or what?

best regards, Reinhard

#include <String.au3>
$nAmount = -89996.31
$sDelimted = _StringAddThousandsSep($nAmount)
MsgBox(64, 'Info', $sDelimted)

$nAmt = '-38849230'
$sDelim = _StringAddThousandsSep($nAmt)
MsgBox(64, 'Info', $sDelim)
Link to comment
Share on other sites

Mmmh,

tried this from the helpfile - only changed to negative values. Gives back positive values.

Known problem or what?

best regards, Reinhard

#include <String.au3>
$nAmount = -89996.31
$sDelimted = _StringAddThousandsSep($nAmount)
MsgBox(64, 'Info', $sDelimted)

$nAmt = '-38849230'
$sDelim = _StringAddThousandsSep($nAmt)
MsgBox(64, 'Info', $sDelim)
As you can see from the String.au3 include file, the regular expression used in _StringAddThousandsSep() does not allow for any non digits in front of the number in $sString variable.

I was not aware of this problem, feature or whatever.

Modified _StringAddThousandsSep() from String.au3.

#include <String.au3>
$nAmount = -89996.31
$sDelimted = StringAddThousandsSep($nAmount)
MsgBox(64, 'Info', $sDelimted)

$nAmt = '38849230'
$sDelim = StringAddThousandsSep($nAmt)
MsgBox(64, 'Info', $sDelim)

Func StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")
    Local $sResult = "" ; Force string
    ;Local $aNumber = StringRegExp( $sString,  "(\d+)\D?(\d*)", 1) ; Existing RegExp in String.au3
    Local $aNumber = StringRegExp($sString, "(\D?\d+)\D?(\d*)", 1) ; This one works for negatives.
    If UBound($aNumber) = 2 Then
        Local $sLeft = $aNumber[0]
        While StringLen($sLeft)
            $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
            $sLeft = StringTrimRight($sLeft, 3)
        WEnd
        $sResult = StringTrimLeft($sResult, 1) ; Strip leading thousands separator
        If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]
    EndIf
    Return $sResult
EndFunc   ;==>_StringAddThousandsSep
Link to comment
Share on other sites

Thanks for you quick response.

I use the function in a bigger homebanking tool and wondered about the positive development of my accounts :-).

I've updated it from an old version to the new version and replaced old _StringAddComma with the newer one.

Perhaps a hint in the helpfile - only positive values - would be OK.

Or if you furtheron interested, in following cases you will get errors in later calculations.

best regards, Reinhard

#include <String.au3>
$nAmount = -899.31
$sDelimted = StringAddThousandsSep($nAmount)
MsgBox(64, 'Info', $sDelimted)

$nAmt = '-388492'
$sDelim = StringAddThousandsSep($nAmt)
MsgBox(64, 'Info', $sDelim)

Func StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")
    Local $sResult = ""; Force string
   ;Local $aNumber = StringRegExp( $sString,  "(\d+)\D?(\d*)", 1); Existing RegExp in String.au3
    Local $aNumber = StringRegExp($sString, "(\D?\d+)\D?(\d*)", 1); This one works for negatives.
    If UBound($aNumber) = 2 Then
        Local $sLeft = $aNumber[0]
        While StringLen($sLeft)
            $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
            $sLeft = StringTrimRight($sLeft, 3)
        WEnd
        $sResult = StringTrimLeft($sResult, 1); Strip leading thousands separator
        If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]
    EndIf
    Return $sResult
EndFunc  ;==>_StringAddThousandsSep
Link to comment
Share on other sites

Thanks for you quick response.

I use the function in a bigger homebanking tool and wondered about the positive development of my accounts :-).

I've updated it from an old version to the new version and replaced old _StringAddComma with the newer one.

Perhaps a hint in the helpfile - only positive values - would be OK.

Or if you furtheron interested, in following cases you will get errors in later calculations.

best regards, Reinhard

A simple fix for negative values.

If value in $sString is negative, add minus sign to return string.

Your comment about further calculations necessitated further experimentation.

The only way I could find to use the output of StringAddThousandsSep() in further calculations was to remove the commas from the string values before any calculations.

I think the best solution would be to update _StringAddThousandsSep() in the String.au3 file to allow of negative values.

Can you find any problems with my second attempt at a solution?

#include <String.au3>
$nAmount = -899.31
$sDelimted = StringAddThousandsSep($nAmount)

$nAmt = '-388492'
$sDelim = StringAddThousandsSep($nAmt)
MsgBox(64, 'Info', $sDelim & " - (" & $sDelimted & ") = " & _
        StringAddThousandsSep(StringReplace($sDelim, ",", "") - StringReplace($sDelimted, ",", "")))

Func StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")
    Local $sResult = "", $iNegSign = ""
    If Number($sString) < 0 Then $iNegSign = "-" ; Allows for a negative value
    Local $aNumber = StringRegExp($sString, "(\d+)\D?(\d*)", 1)
    If UBound($aNumber) = 2 Then
        Local $sLeft = $aNumber[0]
        While StringLen($sLeft)
            $sResult = $sThousands & StringRight($sLeft, 3) & $sResult
            $sLeft = StringTrimRight($sLeft, 3)
        WEnd
        $sResult = StringTrimLeft($sResult, 1); Strip leading thousands separator
        If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]
    EndIf
    Return $iNegSign & $sResult ; Adds minus or "" (nothing)
EndFunc   ;==>StringAddThousandsSep
Link to comment
Share on other sites

  • Moderators

Ya'll need to add this to the bug report. Not returning a negative is a bug with this function IMHO.

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

..

Your comment about further calculations necessitated further experimentation.

...

Can you find any problems with my second attempt at a solution?

Now it works perfectly.

I calculate with the downloaded data in Excel and Excel has no problem with that kind of values.

To have to work with regular expressions drop me allways in trouble

and then in a deep depression, so I try to avoid.

Thank you very much for your work on that.

Best regards, Reinhard

Edited by ReFran
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...