Jump to content
Sign in to follow this  
DjDeep00

Simple Addition Help...

Recommended Posts

DjDeep00

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
bluebearr

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
weaponx

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
DjDeep00

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.