Modify

Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#856 closed Bug (Completed)

_StringAddThousandsSep() prefixes some #'s with ,

Reported by: Ascendant Owned by:
Milestone: Future Release Component: AutoIt
Version: 3.3.0.0 Severity: None
Keywords: _StringAddThousandsSep Cc:

Description

During testing, this UDF sometimes puts a ',' at the beginning of a number which isn't correct. For example, the below results in -,123,456,789,012,345.67890:

#include <String.au3>
MsgBox(16,"_StringAddThousandsSep() faulty reult", _
	"Result of _StringAddThousandsSep('-123456789012345.67890'): "& _
	_StringAddThousandsSep('-123456789012345.67890'))

_DebugBugReportEnv() Result:

Environment = 3.3.0.0 under WIN_XP/Service Pack 3 X86

Attachments (0)

Change History (3)

comment:1 Changed 10 years ago by Gary

  • Resolution set to Completed
  • Status changed from new to closed

This was fixed after the release version. Should be in next beta.

Func _StringAddThousandsSep($sString, $sThousands = -1, $sDecimal = -1)
	Local $sResult = "", $sNegative = "" ; Force string
	Local $rKey = "HKCU\Control Panel\International"
	If $sDecimal = -1 Then $sDecimal = RegRead($rKey, "sDecimal")
	If $sThousands = -1 Then $sThousands = RegRead($rKey, "sThousand")
	Local $aNumber = StringRegExp($sString, "(\D?\d+)\D?(\d*)", 1) ; This one works for negatives.
	If UBound($aNumber) = 2 Then
		Local $sLeft = $aNumber[0]
		If StringLeft($sLeft, 1) = "-" Then
			$sNegative = "-"
			$sLeft = StringTrimLeft($sLeft, 1)
		EndIf
		While StringLen($sLeft)
			$sResult = $sThousands & StringRight($sLeft, 3) & $sResult
			$sLeft = StringTrimRight($sLeft, 3)
		WEnd
		$sResult = $sNegative & StringTrimLeft($sResult, StringLen($sThousands)) ; Strip leading thousands separator
		If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]
	EndIf
	Return $sResult
EndFunc   ;==>_StringAddThousandsSep

comment:2 Changed 10 years ago by TicketCleanup

  • Milestone set to Future Release

Automatic ticket cleanup.

comment:3 Changed 10 years ago by Spiff59

This version, incorporates the fix required above, and also strips off leading zeros, as returning a string such as "00,000,015,746" seems improper. This version is also more than twice as fast as the exisitng one. It retains all functionality, but will return @error = 1 if an invalid string is passed to the function. Presently, the first non-numeric character passed is replaced by a decimal point, and the passed string is truncated at the second non-numeric character. Rather than that "unpredictable" or undocumented behavior, I would think an @error code would be preferable.
{{{Func StringAddThousandsSep($sText, $Sep = ',', $Dec = '.')

If Not StringIsInt($sText) And Not StringIsFloat($sText) Then Return SetError(1)
Local $aSplit = StringSplit($sText, "-" & $Dec )
Local $iInt = 1
Local $iMod
If Not $aSplit[1] Then

$aSplit[1] = "-"
$iInt = 2

EndIf
If $aSplit[0] > $iInt Then

$aSplit[$aSplit[0]] = "." & $aSplit[$aSplit[0]]

EndIf
$iMod = Mod(StringLen($aSplit[$iInt]), 3)
If Not $iMod Then $iMod = 3
$aSplit[$iInt] = StringRegExpReplace($aSplit[$iInt], '(?<=\d{' & $iMod & '})(\d{3})', $Sep & '\1')
For $i = 2 to $aSplit[0]

$aSplit[1] &= $aSplit[$i]

Next
Return $aSplit[1]

EndFunc
}}}

Guidelines for posting comments:

  • You cannot re-open a ticket but you may still leave a comment if you have additional information to add.
  • In-depth discussions should take place on the forum.

For more information see the full version of the ticket guidelines here.

Add Comment

Modify Ticket

Action
as closed The ticket will remain with no owner.
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.