Jump to content

StringFormat help/examples


Recommended Posts

Although I don't currently need any help with this function, as I have solved my current problem, I do believe there should be a lot more documentation on this function with simple and BARELY complex examples. I opened a feature request ticket for future help files: http://svn.autoitscript.com/trac/ticket/218, and I believe jpm will provide some more examples in the next help file; but I doubt, in consideration of the many different variations and capabilities of this function, that he will provide enough so this function will be THAT much easier to understand. I found this very useful in that it cut a couple of my small number formatting functions down by up to five lines, and I think there are more places I could use it if I understood it better. I agree the function is complicated, but that is the reason for this post. Everything is complicated the first time you try to wrap your head around it...I remember I couldn't figure heads or tails with arrays, now my app is nearly made entirely of arrays, because once you understand the "complicated" stuff, it usually makes your/my app simpler.

The concept of this post is coming from when I was first learning php and was reading php.net and their language reference section where people would jump in and give examples of a function. It helped me learn and apply certain functions much faster by seeing many different examples of the same thing.

So here is my proposal/request: If everyone who knows this function, post a few small examples with a brief explanation/comments so this could be a go to spot in addition to whatever jpm provides in the help...he could probably use some for the help as well.

Just a little idea to help the community.

My two useless/redundant cents:

;I use this to make sure there is a "0" at the end of a dollar amount with cents that end with a "0"....pretty much out the help file....although I can't remember why I put the 4 there, which is essentially another good reason for this post:)
StringFormat( "%4.2f",$TotalCost)

Please contribute.

Link to comment
Share on other sites

I wish AutoIt followed more PHP functions like sprintf...sprintf allows binary representation and parameter re-use so you can insert the same string at multiple locations.

http://us.php.net/sprintf

Here you go.

;Script: String Format Examples
;Author: WeaponX

;////////////////////////////////
;// Convert FileGetTime result
;////////////////////////////////
$Date = FileGetTime(@WindowsDir & "\notepad.exe", 1, 0) 
;Format date for use with Date UDF (YYYY/MM/DD HH:MM:SS)
$fDate = StringFormat("%s/%s/%s %s:%s:%s", $Date[0],$Date[1],$Date[2],$Date[3],$Date[4],$Date[5])
ConsoleWrite("Date format: " & $fDate & @CRLF)

;////////////////////////////////
;// Hex display
;////////////////////////////////
$hex = 0xAABBCC
;Format hex string for display
$fHex = StringFormat("%#x", $hex)
ConsoleWrite("Hex format: " & $fHex & @CRLF)

;////////////////////////////////
;// Decimal precision
;////////////////////////////////
$double = 5.12
;Format float to 4 decimal places
$fDouble = StringFormat("%.4f", $double)
ConsoleWrite("Decimal precision: " & $fDouble & @CRLF)

;////////////////////////////////
;// Integer padding
;////////////////////////////////
$integer = 55
;Pad integer with zeros to 4 digits
$fInteger = StringFormat("%04i", $integer)
ConsoleWrite("Integer padding: " & $fInteger & @CRLF)

;////////////////////////////////
;// String justification
;////////////////////////////////
$s = 'string'
$t = 'many strings'

ConsoleWrite(StringFormat("[%s]",      $s) & @CRLF); // standard string output
ConsoleWrite(StringFormat("[%10s]",    $s) & @CRLF); // right-justification with spaces
ConsoleWrite(StringFormat("[%-10s]",   $s) & @CRLF); // left-justification with spaces
ConsoleWrite(StringFormat("[%010s]",   $s) & @CRLF); // zero-padding works on strings too
ConsoleWrite(StringFormat("[%10.10s]", $t) & @CRLF); // left-justification but with a cutoff of 10 characters
Edited by weaponx
Link to comment
Share on other sites

That's great, thanks, through those examples I've already been able to reduce the code on my current app by 22 lines in total...not that much in respect of the rest of the code, but good enough, and neater. I'm going to play with it and experiment a little more.

Anybody else with examples?

Edited by Champak
Link to comment
Share on other sites

Hi,

lets turn it around.

You give some tasks to go for and we'll post the solutions.

Mega

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

I have no specific tasks as I do not know the full benefits of this function (That's like you telling me an answer, and expecting a question....I was never too good at Jeopardy), nor have a current SPECIFIC need for it. I just have a "feeling" there is a lot more that can be done with it...seeing how it has already shortened parts of my script; and from the examples weaponx has provided shows me ways of doing things that I would have TOTALLY gone in another direction that would have been a little messier. So this topic, as stated in the first post, is strictly for people who are in the know to hopefully just provide simple examples/scripts as weaponx did to show utilizations, since the help currently doesn't really provide "clear" help.

EDIT:

As a matter of fact, I do have a task, can stringformat remove ONLY a leading "0" IF it exists?

Edited by Champak
Link to comment
Share on other sites

In that case I would just do...

$string = "05566"
If StringLeft($string, 1) = "0" Then $string = StringTrimLeft($string, 1)

or maybe

$num = 05566
 If StringLeft($num, 1) = "0" Then $num = StringTrimLeft($num, 1)

or...

$string = "0555"
$string = StringRegExpReplace($string,"^0", "")
MsgBox(0,"",$string)
Edited by weaponx
Link to comment
Share on other sites

$string = "05566"
ConsoleWrite(Int($string) & @CRLF)
ConsoleWrite(StringFormat('%d', $string) & @CRLF)

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Link to comment
Share on other sites

  • 6 years later...

Hello, I need help with string formatting...

I want to use a label on the GUI as output text, but I can't solve the word wrap for some reason. How can I give the parameter to the string, to put a @CRLF after every 65th character? (The ouptut field is with fixed with font)

Cheers!

:ILA2:

Link to comment
Share on other sites

  • Developers

Hello, I need help with string formatting...

I want to use a label on the GUI as output text, but I can't solve the word wrap for some reason. How can I give the parameter to the string, to put a @CRLF after every 65th character? (The ouptut field is with fixed with font)

Cheers!

:ILA2:

Not sure why you needed to resurrect a 7 year old thread to ask this question, as it feels to me there is no relation to the StringFormat() function.

What about you show what you have and what is going wrong so it's easier for us to understand the question you are asking?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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