Search Unity

String.Format

Discussion in 'Scripting' started by drJones, Dec 24, 2006.

  1. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    does anyone know if this works in javascript or does it only work in c#?

    i'm trying to pad numbers with zeros, and any code i've grabbed from googling doesn't seem to work. unity doesn't throw any errors at me - it just isn't formatting anything, just displaying the format code i've been trying.
     
  2. NCarter

    NCarter

    Joined:
    Sep 3, 2005
    Posts:
    686
    number.ToString("0000") works well for turning a number into a string and padding it with zeroes. If number is 12, for example, the string would be 0012. This works in Javascript as well as C#, I believe.
     
    BilalMaqsoodQazi likes this.
  3. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    awesome, it works great. thanks ; )
     
  4. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    String.Format should also work fine. Do you have an example of code using String.Format you are having problems with? If you post it here, I could take a look at it to see what's going wrong.

    There are a few examples of code using String.Format in the Unify Wiki
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I always use .ToString (which is probably the same function, or calls the same function)

    hours.ToString("#0") + ":" + minutes.ToString("00") + seconds.ToString("00.00")
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    String.Format is bit more effective than concatenating strings using ToString and the + operator. Every time you use the + to concatenate 2 strings, the runtime has to allocate a new string and copy the two strings into that. Statements like s1 + s2 + s3 will first allocate a new string for the result of s1 + s2 and then another one for (s1 + s2) + s3.

    String.Format only allocates a single string for the output and copies all the input parameters into that one. Also having the format string at the beginning separates the format from the data, potentially making your code more readable
     
  7. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    i was trying to figure out if it worked in javascript. i didn't bookmark anything but the last line i tried that i had left commented out in my script is:

    ammoC = String.Format("%4.3d", ammoCount);

    i don't remember what else i tried (it was before the holidays - its all a blur now ; ) but it was all somewhat similar to that.
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    It's seems you are tring to use C printf format strings. The format strings for String.Format are different. See the documentation on String.Format on msdn for more information.

    "%4.3d" in your example should be "{0:####.000}"
     
  9. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    yeah, that above code was just plain wrong on my part. thanks to your help i found what i needed to pad my number with zeros.

    i have one last question regarding this. in my specific case with this string i am only formatting that one parameter - so would it not be better to use NCarter's method here as it uses less code? (this of course is presuming i coded String.Format efficiently - not a safe bet ; )

    the two versions:

    Code (csharp):
    1.  
    2. function UpdateGUI ()
    3. {
    4.     if (weaponScript)
    5.     {
    6.         ammoCount = weaponScript.GetBulletsLeft();
    7.         ammoC = String.Format("{0:000}", ammoCount);
    8.         ammoGUI.text = ammoC.ToString();
    9.     }
    10. }
    11.  
    Code (csharp):
    1.  
    2. function UpdateGUI ()
    3. {
    4.     if (weaponScript)
    5.     {
    6.         ammoGUI.text = weaponScript.GetBulletsLeft().ToString("000");
    7.     }
    8. }
    9.  
     
  10. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    little practical difference (although the .ToString() in the first does nothing). Just use whatever you like the most.
     
  11. drJones

    drJones

    Joined:
    Oct 19, 2005
    Posts:
    1,351
    ok great, thanks guys : )