Jump to content
Alan1234

Leading Zero

Recommended Posts

Alan1234

After using Int(expression) message box shows a number, for example the number 6.  How can I show a leading zero to represent the Tens digit, for example the number 06?

Share this post


Link to post
Share on other sites
water

Try function StringFormat.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
Alan1234

If I use StringFormat won't that make it a string?  I want the result to be a number with a leading zero.  Does StringFormat do this?

Share this post


Link to post
Share on other sites
Jos

Yes it makes it a string as values never have leading zero's. So what are you trying to solve here?

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites
Alan1234

Just trying to show an integer below the number 10 with a leading zero to represent the Tens digit or hundreds digit like in Excel.  From your comment I take it that AutoIt cannot do this.

Share this post


Link to post
Share on other sites
water

The internal representation of a number and the format when displaying this number are two different things (as they are in Excel as well).
Use StringFormat before you display the integer. This does not affect the internal representation.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites
AspirinJunkie
$d_Number = 6
$s_Number = StringRight("00" & $d_Number, StringLen($d_Number) > 2 ? StringLen($d_Number) : 2)
MsgBox(0,"", $s_Number)

$d_Number = 15
$s_Number = StringRight("00" & $d_Number, StringLen($d_Number) > 2 ? StringLen($d_Number) : 2)
MsgBox(0,"", $s_Number)

$d_Number = 115
$s_Number = StringRight("00" & $d_Number, StringLen($d_Number) > 2 ? StringLen($d_Number) : 2)
MsgBox(0,"", $s_Number)

 

Share this post


Link to post
Share on other sites
Alan1234

Agreed! Thanks for your comments.

Aspirinjunkie:  Thanks for example scripts.

Edited by Alan1234

Share this post


Link to post
Share on other sites
Chimp

using StringFormat (as already suggested in post #2)

Local $iWantedDigits = 2 ; <-- choose wanted length

For $i = 0 To 15
    ConsoleWrite(_Leading0($iWantedDigits, $i) & @CRLF)
Next

; returns the passed number formatted
; with a length of $iNrDigits with leading zero
Func _Leading0($iNrDigits, $iNum)
    Return StringFormat('%0' & $iNrDigits & 's', $iNum)
EndFunc   ;==>_Leading0

 

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
AspirinJunkie

omg - i mixed up two different cases (leading zeros vs. trailing zeros).
Yes of course - stringformat is the preferred choice for the case of leading zeros.

@Alan1234
Don't be confused with my "solution" - use stringformat as already mentioned.

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

×

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.