huldu Posted March 1, 2006 Posted March 1, 2006 I was sitting here manopulating strings like ive been doing the past few days hehe. Some problems, even i can solve thanks to the help file But however, from time to time some hard nut pops up i cant crack on my own. Now ive ran into a problem once again. I used StringReplace() to remove "," from an integer so i could add them together for a total. Piece of cake. Now i need to put the "," back into the number The problem is the string manipulation would haveto start from the 3 left most characters and continue add a "," until it reach the start of the number. For example 1000000, would be converted to 1,000,000 and 1000 would be converted to 1,000. To be honest i dont know how to do this in an "easy" way, maybe its not that easy in the first place "I'm paper, rock is fine, nerf scissors!!!"
GaryFrost Posted March 1, 2006 Posted March 1, 2006 (edited) Function by Guest expandcollapse popup#include <Array.au3> $number = "1000" $number = _AddComma($number) MsgBox(0,"test",$number) ;================================================= ; Function Name: _AddComma() ; Description: Add comma's to an array/integer of numbers. ; Return Value(s): On Success: A new array/integer with the value(s) delimited by comma's. ; Return Value(s): On Failure: ; @Error: = 0 = No error. ; 1 = $nValue isn't a number or the array contains no numbers. ;================================================= Func _AddComma($nValue) Local $sComma, $sError = 0 If IsArray($nValue) Then For $I = 0 To UBound($nValue) - 1 If StringIsAlpha($nValue) Then $sError = $sError + 1 If $sError = UBound($nValue) Then SetError(1) Return (0) EndIf EndIf Next For $I = 0 To UBound($nValue) - 1 If StringIsDigit($nValue[$I]) And StringLen($nValue[$I]) > 3 Then $sComma = StringLeft($nValue[$I], StringLen($nValue[$I]) - 3) & "," & _ StringRight($nValue[$I], 3) Do If Not StringInStr(StringLeft($sComma, 4), ",") Then $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _ StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4) EndIf Until StringInStr(StringLeft($sComma, 4), ",") $nValue[$I] = $sComma EndIf Next Return $nValue Else If Not StringIsDigit($nValue) And Not StringIsFloat($nValue) Then SetError(1) Return (0) Else If StringIsFloat($nValue) Then Local $Split = StringSplit($nValue, ".") $nValue = $Split[1] EndIf If StringLen($nValue) > 3 Then $sComma = StringLeft($nValue, StringLen($nValue) - 3) & "," & StringRight($nValue,3) Do If Not StringInStr(StringLeft($sComma, 4), ",") Then $sComma = StringLeft($sComma, StringInStr($sComma, ",") - 4) & "," & _ StringRight($sComma, StringLen($sComma) - StringInStr($sComma, ",") + 4) EndIf Until StringInStr(StringLeft($sComma, 4), ",") If IsDeclared("Split") Then $sComma = $sComma & "." & $Split[2] EndIf Return $sComma EndIf EndIf EndIf EndFunc Edited February 19, 2019 by Jon SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
huldu Posted March 1, 2006 Author Posted March 1, 2006 (edited) Great, thanks! Just what i were looking for. Is it possible to save user made functions somewhere so i can call them from my script? <edit>I put it in the beta \include dir and added #include-once to it, seems to work <edit> Edited March 1, 2006 by huldu "I'm paper, rock is fine, nerf scissors!!!"
Gene Posted March 1, 2006 Posted March 1, 2006 I was sitting here manopulating strings like ive been doing the past few days hehe. Some problems, even i can solve thanks to the help file But however, from time to time some hard nut pops up i cant crack on my own. Now ive ran into a problem once again.I used StringReplace() to remove "," from an integer so i could add them together for a total. Piece of cake.Now i need to put the "," back into the number The problem is the string manipulation would haveto start from the 3 left most characters and continue add a "," until it reach the start of the number. For example 1000000, would be converted to 1,000,000 and 1000 would be converted to 1,000. To be honest i dont know how to do this in an "easy" way, maybe its not that easy in the first place This is not elegant, but it works...Put the Value into $iTmpVarIf StringLen of $iTmpVar > 3 thenUse StringRight and StringTrimRight to cut the last 3 digits of $iTmpVar into $iTmpVar01Concantenate $iTmpVar01 = "," & $iTmpVar01While StringLen of $iTmpVar > 0 Use StringRight and StringTrimRight to cut the last 3 digits of $iTmpVar into $iTmpVar02 Concantenate $iTmpVar01 = "," & $iTmpVar02 Get the StringLen of $iTmpVarWendEndifSemiSuedoCodeYou'd also need some more code to deal with decimals if you would ever have them.Gene [font="Verdana"]Thanks for the response.Gene[/font]Yes, I know the punctuation is not right...
GaryFrost Posted March 1, 2006 Posted March 1, 2006 (edited) Gene said: This is not elegant, but it works... Put the Value into $iTmpVar If StringLen of $iTmpVar > 3 then Use StringRight and StringTrimRight to cut the last 3 digits of $iTmpVar into $iTmpVar01 Concantenate $iTmpVar01 = "," & $iTmpVar01 While StringLen of $iTmpVar > 0 Use StringRight and StringTrimRight to cut the last 3 digits of $iTmpVar into $iTmpVar02 Concantenate $iTmpVar01 = "," & $iTmpVar02 Get the StringLen of $iTmpVar Wend Endif SemiSuedoCode You'd also need some more code to deal with decimals if you would ever have them. Gene The one I posted by Guest already works with int/floats i.e. $number = "1990000.50" $number = _AddComma($number) MsgBox(0,"test",$number) Edited February 19, 2019 by Jon SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
seandisanti Posted March 1, 2006 Posted March 1, 2006 (edited) Good function, but it seems like it could be done with less code, or even a single line using regexp... i don't use regexp (and i DID even try to get the hang of them but gave up on them) but i think it could still be shorter to do something like this... p.s. don't tear this apart too much, just whipped this up real fast while working on other stuff.... $anumber = 100000000.5347 $anumber = Commanate($anumber) MsgBox(0,"blah",$anumber) Func Commanate($n) $number = "" $NoDec = StringLeft($n,StringLen($n)-(StringLen($n)-StringInStr($n,".")+1)) ConsoleWrite(@LF & $NoDec & @LF) $DecCount = StringLen($NoDec)/3 ConsoleWrite(@LF & $DecCount & @LF) If $DecCount Then For $x = 1 to $DecCount $number = "," & StringRight($NoDec,3) & $number $NoDec = StringLeft($NoDec,StringLen($NoDec)-3) Next If Not Mod(StringLen($NoDec),3) Then $number = StringRight($number,StringLen($number)-1) $number = $NoDec & $number EndIf If StringInStr($n,".") Then $number = $number & StringRight($n,StringLen($n) - StringInStr($n,".")+1) EndIf Return $number EndFunc Edited February 19, 2019 by Jon
flyingboz Posted March 2, 2006 Posted March 2, 2006 good function, but it seems like it could be done with less code,A couple of lines shorter, same basic idea, but with stringsplit() and demonstrating use of Eval() $num = 1789.00009 if StringInStr($num,'.') Then $num = StringSplit($num,'.') $int = $num[1] $frac = $num[2] Else $int = $num EndIf $result = "" While StringLen($int) > 3 $result = ',' & StringStripWs(StringRight($int,3),3) & $result $int = StringTrimRight($int,3) WEnd $int = StringFormat('%s%s%s',$int,$result,'.' & Eval("frac")) If StringRight($int,1) = '.' Then $int = StringTrimRight($int,1) Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.
GaryFrost Posted March 2, 2006 Posted March 2, 2006 (edited) longer than flyingboz code, but shorter than Guest's function, also supports decimal precision and number passed in can be number or string number. expandcollapse popupOpt('MustDeclareVars', 1) Local $num = 1000000.50 MsgBox(0, "test", _AddComma($num, 2)) MsgBox(0, "test", _AddComma($num, 1)) MsgBox(0, "test", _AddComma($num)) $num = 1000000.00 MsgBox(0, "test", _AddComma($num)) MsgBox(0, "test", _AddComma($num, 1)) MsgBox(0, "test", _AddComma($num, 2)) $num = "1000000.00" MsgBox(0, "test", _AddComma($num)) MsgBox(0, "test", _AddComma($num, 1)) MsgBox(0, "test", _AddComma($num, 2)) $num = 1000000 MsgBox(0, "test", _AddComma($num)) MsgBox(0, "test", _AddComma($num, 1)) MsgBox(0, "test", _AddComma($num, 2)) $num = "1000000" MsgBox(0, "test", _AddComma($num)) MsgBox(0, "test", _AddComma($num, 1)) MsgBox(0, "test", _AddComma($num, 2)) Func _AddComma($v_num, $i_decimail_precision = 0) Local $new_num, $frac = "", $result = "" If (Not StringIsInt($v_num)) And (Not StringIsFloat($v_num)) Then SetError(1) Return "Non-numerical value passed into _AddComma" EndIf If $i_decimail_precision < 0 Then $i_decimail_precision = 0 If StringInStr($v_num, '.') Then $v_num = StringSplit($v_num, '.') $new_num = $v_num[1] If $i_decimail_precision Then $frac = '.' & $v_num[2] Else $new_num = $v_num EndIf While StringLen($new_num) > 3 $result = ',' & StringStripWS(StringRight($new_num, 3), 3) & $result $new_num = StringTrimRight($new_num, 3) WEnd If $i_decimail_precision Then If StringLen($frac) < $i_decimail_precision + 1 Then If Not StringLen($frac) Then $frac = '.' For $x = 1 To ($i_decimail_precision + 1) - StringLen($frac) $frac = $frac & '0' Next ElseIf StringLen($frac) > $i_decimail_precision + 1 Then $frac = StringTrimRight($frac, StringLen($frac) - ($i_decimail_precision + 1)) EndIf EndIf $new_num = StringFormat('%s%s%s', $new_num, $result, $frac) Return $new_num EndFunc ;==>_AddComma Edited February 19, 2019 by Jon SciTE for AutoItDirections for Submitting Standard UDFs Don't argue with an idiot; people watching may not be able to tell the difference.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now