Sign in to follow this  
Followers 0
DjDeep00

Simple Addition Help...

4 posts in this topic

The result I get from the following code is "Test_2" but I am trying to get "Test_0002".

Sorry in advance, for this silly question.... :)

$Number='Test_0001'
$Number_Split=StringSplit($Number,"_")

$Next_Number=$Number_Split[1] & "_" & $Number_Split[2]+1

MsgBox(4096,"",$Next_Number)

Share this post


Link to post
Share on other sites



When you add 1 to the string '001', AutoIt converts the string ('001') to a number to perform the addition. Numbers don't have leading zeros, and so they don't show up in the result.

Use something like this:

$Number='Test_0001'
$Number_Split=StringSplit($Number,"_")

$Next_Number=$Number_Split[1] & "_" & _Pad($Number_Split[2]+1, 3)

MsgBox(4096,"",$Next_Number)

Func _Pad($num, $len)
    Local $zeros = '00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
    Return StringLeft($zeros, $len - Stringlen($num)) & $num
EndFunc

BlueBearrOddly enough, this is what I do for fun.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

StringFormat has built-in precision flags for leading and trailing digits

$Number='Test_0001'
$Number_Split=StringSplit($Number,"_")

;%04d pads to 4 places, %03d would pad to 3
$Next_Number=$Number_Split[1] & "_" & StringFormat("%04d", $Number_Split[2] + 1)

MsgBox(4096,"",$Next_Number)
Edited by weaponx

Share this post


Link to post
Share on other sites

Beautiful...Thanx guys.

Share this post


Link to post
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
Sign in to follow this  
Followers 0