Jump to content

Number of characters


Recommended Posts

I am trying to grab a number from somewhere, and I want that if the number is less than 6 digits, to fill the rest with 0's. So if the digit is 1234, I want to grab 001234.

Hard way:

#include <array.au3>
Local $array[5] = ["123456","123","1","1234567","12345"]

For $i=0 to UBound($array)-1
    $len=StringLen($array[$i])
    If $len < 6 Then
        For $j=$len+1 To 6
            $array[$i]= 0 & $array[$i]
        Next
    EndIf
Next

_ArrayDisplay($array)

Easy way?:

#include <array.au3>
Local $array[5] = ["123456","123","","1234567","12345"]

For $i=0 to UBound($array)-1
    $array[$i]=StringFormat("%06s",$array[$i])
Next

_ArrayDisplay($array)
Link to comment
Share on other sites

Thanks, thats the easy part. So far I have:

$number=inputbox("","","")

if stringlen($number) < 6 Then
    
    $amountof0s= 6-stringlen($number)
    msgbox(0,"", $amountof0s&$number)
    EndIf

This gives me the number of 0's, not the actual 0's. For example if I put 123, it will say "3123". How can I get it to say "000123"? Thanks again :unsure:

Link to comment
Share on other sites

Hard way:

#include <array.au3>
Local $array[5] = ["123456","123","1","1234567","12345"]

For $i=0 to UBound($array)-1
    $len=StringLen($array[$i])
    If $len < 6 Then
        For $j=$len+1 To 6
            $array[$i]= 0 & $array[$i]
        Next
    EndIf
Next

_ArrayDisplay($array)

Easy way?:

#include <array.au3>
Local $array[5] = ["123456","123","","1234567","12345"]

For $i=0 to UBound($array)-1
    $array[$i]=StringFormat("%06s",$array[$i])
Next

_ArrayDisplay($array)

Thanks for this. I can see that it works with the given array, but how can we make that array a variable?
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...