Thread Tools Display Modes
06-10-13, 11:11 AM   #1
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
make integer 01 instead of 1

I'm trying to make and integer "01" instead of "1" but it's not working, here's what i've been trying

Lua Code:
  1. int=tonumber("0"..int)

but it still returns 1
  Reply With Quote
06-10-13, 11:24 AM   #2
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
You may well have to store it as a string.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
06-10-13, 11:26 AM   #3
Malsomnus
A Cobalt Mageweaver
AddOn Author - Click to view addons
Join Date: Apr 2013
Posts: 203
Sounds like you want a string, and not a number. You should remove the tonumber call.
  Reply With Quote
06-10-13, 11:45 AM   #4
Spawnova
A Warpwood Thunder Caller
 
Spawnova's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2012
Posts: 96
I didn't know you could add one string to another, that works =)
  Reply With Quote
06-10-13, 11:56 AM   #5
Dridzt
A Pyroguard Emberseer
 
Dridzt's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2005
Posts: 1,360
("%02d"):format(1) -> '01'
("%05d"):format(25) -> '00025'

Or let's say you want right-aligned strings with spaces instead of zeros.
("% 5d"):format(1)
("% 5d"):format(11)
("% 5d"):format(1111) ->
Code:
    1
   11
 1111

Last edited by Dridzt : 06-10-13 at 12:08 PM.
  Reply With Quote
06-10-13, 09:19 PM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Spawnova View Post
I didn't know you could add one string to another, that works =)
Just FYI that's not addition. Addition is a mathematical operation (eg. 2 + 2 -> 4) performed on numbers. The joining of strings together (eg. "a" .. "b" -> "ab") is called concatenation.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
06-11-13, 05:40 AM   #7
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
format is a great tool for simple things, you can deal with a lot of stuff like argument order (for locale, making it much easier to translate if the order of the inserted variables change from language to language), alter how float and numbers appear, even append spacing to strings.
__________________
Profile: Curse | Wowhead
  Reply With Quote
06-11-13, 03:34 PM   #8
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Vlad View Post
format is a great tool for simple things, you can deal with a lot of stuff like argument order (for locale, making it much easier to translate if the order of the inserted variables change from language to language), ...
To clarify, because this is really useful, here's an example:

Code:
format("%1$s's %2$s is %3$s", "John", "cat", "happy")
-> John's cat is happy.
Pretty straightforward for English. But what about other languages where the order of words is different?

Code:
format("%2$s de %1$s está %3$s.", "Juan", "El gato", "feliz")
-> El gado de Juan está feliz.
So, you could assign the format pattern to, say, L["FormatPattern"] and localizers change the order of words appropriately. You don't need to know which order that is -- you just pass in the variables in the same order you would for English.

You don't have to use the %s token only, or exclusively; you can mix any format tokens, as long as they have <number>$ between the % and the token key: "%1$s has %2$d for %3$0.2f" works fine.

You'll see this used in many of Blizzard's combat log strings.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote
06-11-13, 04:03 PM   #9
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by Phanx View Post
To clarify, because this is really useful, here's an example:
[...]
Can you explain why/how this works in the WoW Lua, but not in the normal Lua?

I tried it on the Live Demo and the (5.2) binary
The Wowpedia format() page also doesn't mention this use

I never realized this functionality until now and I'm wondering if there is more documentation about it
  Reply With Quote
06-11-13, 06:02 PM   #10
Torhal
A Pyroguard Emberseer
 
Torhal's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2008
Posts: 1,196
This is a forward port by the WoW team from Lua 4.0 to their version of Lua 5.1. Here's the relevant page in the Lua 4.0 Reference Manual.

EDIT: For those who just want the juicy bits:

Conversions can be applied to the n-th argument in the argument list, rather than the next unused argument. In this case, the conversion character % is replaced by the sequence %d$, where d is a decimal digit in the range [1,9], giving the position of the argument in the argument list. For instance, the call format("%2$d -> %1$03d", 1, 34) will result in "34 -> 001". The same argument can be used in more than one conversion.
__________________
Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Author of NPCScan and many other AddOns.

Last edited by Torhal : 06-11-13 at 06:10 PM.
  Reply With Quote
06-11-13, 06:06 PM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Originally Posted by Ketho View Post
Can you explain why/how this works in the WoW Lua, but not in the normal Lua?
It's the same reason why string.trim works in WoW Lua, but not in normal Lua -- because Blizzard added it there.
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » make integer 01 instead of 1


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off