Jump to content

This site uses cookies. By continuing to browse the site you are agreeing to our use of cookies. Find out more here. X
X


Photo

_StringAddComma GERMAN


  • This topic is locked This topic is locked
54 replies to this topic

#41 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 16,056 posts

Posted 28 February 2008 - 05:47 PM

Ron, there's a couple of problems with that:

  • It's Thousands, not Thousandths
  • You have hard-coded checks for [,.] but you can't, it's up to the user to tell you what the decimal separator is.
Here. This is simple and it works:
Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = "" ; Force string     Local $aNumber = StringRegExp($sString, "(\d+)(\x" & Hex(Asc($sDecimal), 2) & "?\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 &= $aNumber[1]     EndIf     Return $sResult EndFunc


Tested with:
Local $s1 = _StringAddThousandsSep(123456.654321) ConsoleWrite('@@ (1) :(' & @min & ':' & @sec & ') $s1 = ' & $s1 & @CRLF);### Debug Console Local $s2 = _StringAddThousandsSep(StringReplace(123456.654321, ".", ","), ".", ",") ConsoleWrite('@@ (3) :(' & @min & ':' & @sec & ') $s2 = ' & $s2 & @CRLF);### Debug Console Local $s3 = _StringAddThousandsSep(StringReplace(123456.654321, ".", "|"), "^", "|") ConsoleWrite('@@ (5) :(' & @min & ':' & @sec & ') $s3 = ' & $s3 & @CRLF);### Debug Console Local $s4 = _StringAddThousandsSep(123456) ConsoleWrite('@@ (7) :(' & @min & ':' & @sec & ') $s4 = ' & $s4 & @CRLF);### Debug Console


Edit: Fixed mistake in code.

Hmm, take a look at this:
AutoIt         
;Valik Sleep(2000) $nTimer = TimerInit() $nAmount = "89996312321.30" ConsoleWrite("'g', 'a': " & $nAmount & @lf & __StringAddThousandsSep($nAmount, 'g', 'a') & @LF) $nDiff = TimerDiff($nTimer) / 1000 ConsoleWrite("Time: " & $nDiff & @CRLF & @CRLF) Func __StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""   ; Force string     Local $aNumber = StringRegExp($sString, "(\d+)(\x" & Hex(Asc($sDecimal), 2) & "?\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 &= $aNumber[1]     EndIf     Return $sResult EndFunc ;SmOke_N Sleep(2000) $nTimer = TimerInit() $nAmount = "89996312321.30" ConsoleWrite("'g', 'a': " & $nAmount & @lf & _StringAddThousandsSep($nAmount, 'g', 'a') & @LF) $nDiff = TimerDiff($nTimer) / 1000 ConsoleWrite("Time: " & $nDiff & @CRLF & @CRLF) Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $aEnding = StringRegExp($sString, "\D(\d+)$", 1)     Local $sTemp = $sString, $sBeginning, $sEndReturn, $iCC     If IsArray($aEnding) Then         $aEnding = StringRight($sTemp, StringLen($aEnding[0]) + 1)         $sTemp = StringReplace($sTemp, $aEnding, "")         $sEndReturn = $sDecimal & StringTrimLeft($aEnding, 1)     EndIf     $sTemp = StringRegExpReplace($sTemp, "\D", "")     Local $nLen = StringLen($sTemp)     For $iCC = 1 To Int($nLen / 3)         If $iCC = ($nLen / 3) Then $sThousands = ""         $sBeginning = $sThousands & StringRight($sTemp, 3) & $sBeginning         $sTemp = StringTrimRight($sTemp, 3)     Next     Return StringLeft($sTemp, 3) & $sBeginning & $sEndReturn EndFunc


Edit:
Fixed Code

Edit2:
Damn, fixed it again lol

Edit3:
I see what you did... End user must know what the common delimiters are... makes sense to me.

Edited by SmOke_N, 28 February 2008 - 05:58 PM.

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.








#42 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 28 February 2008 - 06:02 PM

Fine.

#43 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 28 February 2008 - 06:08 PM

Actually, here, this is much simpler to follow than your code:
Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""; Force string     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 $sResult EndFunc


Edit: Changed code slightly, fixed code.

Edited by Valik, 28 February 2008 - 06:16 PM.


#44 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 16,056 posts

Posted 28 February 2008 - 06:12 PM

Actually, here, this is much simpler to follow than your code:

Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""; Force string     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 $sResult EndFunc


Edit: Changed code slightly.

No array returned:
AutoIt         
$nAmount = "89996312321a30" ConsoleWrite("'g', 'a': " & $nAmount & @lf & __StringAddThousandsSep($nAmount, 'g', 'a') & @LF) Func __StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""  ; Force string     Local $aNumber = StringRegExp($sString, "(\d+)\D?\d*)", 1)     _ArrayDisplay($aNumber) ;~ Debugging here, no array returned     If UBound($aNumber) = 2 Then         Local $sLeft = $aNumber[0]         For $i = 1 To Int(StringLen($sLeft) / 3)             $sResult = $sThousands & StringRight($sLeft, 3) & $sResult             $sLeft = StringTrimRight($sLeft, 3)         Next         $sResult = StringTrimLeft($sResult, 1)  ; Strip leading thousands separator         If $aNumber[1] <> "" Then $sResult &= $sDecimal & $aNumber[1]     EndIf     Return $sResult EndFuncƒo݊÷ ØGb´È¬²)àj–«z{azȬjëhŠ×6Func __StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = "" ; Force string     Local $aNumber = StringRegExp($sString, "(\d+)\D?(\d*)", 1);Missing parenthesis here     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

Edited by SmOke_N, 28 February 2008 - 06:13 PM.

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.


#45 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 28 February 2008 - 06:15 PM

Yeah, I missed that, sorry. Mine is faster as well, but only slightly.
Plain Text         
;Valik Local $nLoop = 1000 Local $nAmount = "89996312321.30" Local $sResult, $nDiff Local $nTimer = TimerInit() For $i = 1 To $nLoop     $sResult = __StringAddThousandsSep($nAmount, 'g', 'a') Next $nDiff = TimerDiff($nTimer) / 1000 ConsoleWrite("'g', 'a': " & $nAmount & @lf &  $sResult & @LF) ConsoleWrite("Time: " & $nDiff & " (Valik)" & @CRLF & @CRLF) ;SmOke_N $nAmount = "89996312321.30" $nTimer = TimerInit() For $i = 1 To $nLoop     $sResult = _StringAddThousandsSep($nAmount, 'g', 'a') Next $nDiff = TimerDiff($nTimer) / 1000 ConsoleWrite("'g', 'a': " & $nAmount & @lf &  $sResult & @LF) ConsoleWrite("Time: " & $nDiff & " (Ron)" & @CRLF & @CRLF) Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $aEnding = StringRegExp($sString, "\D(\d+)$", 1)     Local $sTemp = $sString, $sBeginning, $sEndReturn     If IsArray($aEnding) Then         $aEnding = StringRight($sTemp, StringLen($aEnding[0]) + 1)         $sTemp = StringReplace($sTemp, $aEnding, "")         $sEndReturn = $sDecimal & StringTrimLeft($aEnding, 1)     EndIf     $sTemp = StringRegExpReplace($sTemp, "[" & $sThousands & $sDecimal & "]", "")     For $iCC = 1 To Int(StringLen($sTemp) / 3)         $sBeginning = $sThousands & StringRight($sTemp, 3) & $sBeginning         $sTemp = StringTrimRight($sTemp, 3)     Next     Return StringLeft($sTemp, 3) & $sBeginning & $sEndReturn EndFunc Func __StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""  ; Force string     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 $sResult EndFunc


#46 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 28 February 2008 - 06:17 PM

So, is everybody happy with this version of the function?
Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""   ; Force string     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 $sResult EndFunc

Cause I'm really tired of this fucking thread.

#47 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 16,056 posts

Posted 28 February 2008 - 06:18 PM

So, is everybody happy with this version of the function?

Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""   ; Force string     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 $sResult EndFunc

Cause I'm really tired of this fucking thread.

I am :)

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.


#48 Xenobiologist

Xenobiologist

    Xx Code~Mega xX

  • MVPs
  • 4,861 posts

Posted 28 February 2008 - 09:05 PM

I am :)

I'd like to say yes, but I'm not. It is not my intention to fight for this function, but anyhow this is not what I expect.

$nAmount = "8,000.00" ConsoleWrite("',', '.': " & $nAmount & @LF & _StringAddThousandsSep($nAmount, ',', '.') & @LF) ConsoleWrite("'.', ',': " & $nAmount & @LF & _StringAddThousandsSep($nAmount, '.', ',') & @LF & @LF) Func _StringAddThousandsSep($sString, $sThousands = ",", $sDecimal = ".")     Local $sResult = ""   ; Force string     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 $sResult EndFunc


You cut the cents which IMO isn't correct.

Mega

Scripts & functions Organize Includes Let Scite organize the include files *new

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


#49 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 28 February 2008 - 09:30 PM

What you expect is wrong! See previous comment on garbage-in garbage-out. The function expects a NUMBER, not a formatted string that happens to contain numeric data. Big difference. If the input does not return true when passed to IsNumber(), then it's not valid input. Your string does not pass the "IsNumber test" and is thus garbage input.

#50 GaryFrost

GaryFrost

    I don't need your attitude. I have one of my own

  • Developers
  • 7,854 posts

Posted 28 February 2008 - 09:46 PM

I am :)



Just let me know when your done with it.

SciTE for AutoItDirections for Submitting Standard UDFs

Don't argue with an idiot; people watching may not be able to tell the difference.


#51 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 16,056 posts

Posted 28 February 2008 - 10:04 PM

Just let me know when your done with it.

Personally I don't care... My function takes care of the "garbage" input, but Valiks does what I originally intended... I'll leave in ya'lls (the developers) capable hands.

I haven't actually used the function since I made it.

Edit:
Ebonics

Edited by SmOke_N, 28 February 2008 - 10:05 PM.

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.


#52 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 28 February 2008 - 10:14 PM

My question is, why does it need to handle garbage input? The input Xeno is trying to pass it is already formatted. If the goal is to swap the two separators, that's not what this function is for. This function is for ADDING something that's MISSING. Not changing something that's existing. Besides, it can be easily solved:
$nAmount = StringReplace("8,000.00", ",", "")

Problem solved, strip the commas, then the function will get valid input and properly add the separator.

#53 SmOke_N

SmOke_N

    It's not what you know ... It's what you can prove!

  • Moderators
  • 16,056 posts

Posted 29 February 2008 - 08:28 AM

My question is, why does it need to handle garbage input? The input Xeno is trying to pass it is already formatted. If the goal is to swap the two separators, that's not what this function is for. This function is for ADDING something that's MISSING. Not changing something that's existing. Besides, it can be easily solved:

$nAmount = StringReplace("8,000.00", ",", "")

Problem solved, strip the commas, then the function will get valid input and properly add the separator.

You didn't need to go into all that as I'm sure you're well aware of. The fact that your function does what "I" intended, and it was accepted originally for that reason specifically... I'm still at a loss on why this thread had 30+ post.

Please, for the love of God... just put Valiks code in place of my own so we can end a nightmare that I'm still to this moment confused on why we are even discussing it.

It's efficient, and coherent... enough said.

I'm locking this thread... anyone that wants to bitch... Bitch to Valik in a PM.

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.


#54 GaryFrost

GaryFrost

    I don't need your attitude. I have one of my own

  • Developers
  • 7,854 posts

Posted 29 February 2008 - 10:40 AM

You didn't need to go into all that as I'm sure you're well aware of. The fact that your function does what "I" intended, and it was accepted originally for that reason specifically... I'm still at a loss on why this thread had 30+ post.

Please, for the love of God... just put Valiks code in place of my own so we can end a nightmare that I'm still to this moment confused on why we are even discussing it.

It's efficient, and coherent... enough said.

I'm locking this thread... anyone that wants to bitch... Bitch to Valik in a PM.



Committed.

SciTE for AutoItDirections for Submitting Standard UDFs

Don't argue with an idiot; people watching may not be able to tell the difference.


#55 Valik

Valik

    Former developer.

  • Active Members
  • PipPipPipPipPipPip
  • 18,879 posts

Posted 29 February 2008 - 03:39 PM

I'm locking this thread... anyone that wants to bitch... Bitch to Valik in a PM.

Don't bitch to Valik in a PM. Thanks.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users