Jump to content

Autoit equivalent of vba Format function ?


Go to solution Solved by Coded4Decades,

Recommended Posts

I am converting some vbscript and vba programs to autoit and cannot find anything that is close to the vba Format(..) function.

My current solution is to convert the code manually as follows

vba====> filename = Format(Now, """file created in"" mmm ""at"" hh.mm.ss")

autoit==> $filename = "file created in" & @mon & " at " & @hour  & "." & @min & "."  & @sec

That works But, it would certainly simplify my conversion efforts if Autoit had something like Format which handles, dates, times am/pm, numbers, rounding, text percentages etc,

 

 

 

 

 

 

 

Link to comment
Share on other sites

.... But, it would certainly simplify my conversion efforts if Autoit had something like Format which handles, dates, times am/pm, numbers, rounding, text percentages etc,

Also see StringFormat function and its examples in AutoIt help file.

#include <Date.au3>

Local $filename = StringFormat("file created in %02d at %02d.%02d.%02d", @MON, @HOUR, @MIN, @SEC)
ConsoleWrite($filename & @LF)

;Or
Local $filename2 = StringFormat("file created in %02d at %s", @MON, _NowTime(5)) ; _NowTime is one of the functions in "Date.au3" include file.
ConsoleWrite($filename2 & @LF)

;Or
Local $iNum = 12.3456
Local $Places = 3
;Normally, a flag specification is not a variable concatenated inside a variable format, inside the "format control"
;parameter of the StringFormat function.
;That is, %." & $Places & "f" is equivalent to %.3f (see next line).
Local $rounding = StringFormat("The number %g rounded to %d deciminal places %." & $Places & "f", $iNum, $Places, $iNum)
ConsoleWrite($rounding & @LF)

#cs Output:-
file created in 04 at 08.23.07
file created in 04 at 08:23:07
The number 12.3456 rounded to 3 deciminal places 12.346
#ce
Link to comment
Share on other sites

  • Solution

#include <date.au3>
global  $xl  ; make it global so I can add extra Excel functions easily in the future.

    $ans = _xltext(_now(),"mmm mm/dd/yyyy")
    MsgBox(262144, 'Debug line ~' & @ScriptLineNumber, 'Selection:' & @CRLF & ' $ans' & @CRLF & @CRLF & 'Return:' & @CRLF &     $ans) ;### Debug MSGBOX

func _xltext($value, $format)
    if $xl = "" then $xl= ObjCreate("Excel.Application")
    return $xl.worksheetfunction.text(_now(),"mmm mm/dd/yyyy")
endfunc

Thanks for the suggestions, but I'll probably not use either of them.

stringformat looks useful and I may try it for other things, but it clearly does not handle date and times.

date_time_convert udf has the type of data time functions I want, but it is a very complicated routine which kind of bothers me.

I want the best of both worlds, so I decide to use Excel directly.

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...