Jump to content

Simple Addition Help...


Recommended Posts

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)
Link to comment
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.
Link to comment
Share on other sites

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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...