Dynamic STRING Formatting not working

ok I am clearly missing something, because this should work (I think).

// this works fine
var xyz = string.format("%d");


// this attempt to create a dynamic format does not
var fmt = ""%d""; // this assigns the string "%d" to the variable - identical to hard coding it
var xyz = string.format(fmt);
  • Surely you have extra quotes, or is that a forum formatting thing?
  • As gasteropod alluded to, the extra quotes would definitely cause a compile error.

    Other than that, not sure I understand the intent of your code fragment. If the variable "string" is actually a String, then your code won't work.

    String doesn't have a .format() method, so you'll end up calling the Lang.format() method which acts completely differently (see below). The code you posted only makes sense if "string" is a numerical type (Float, Double, Number or Long).

    - [Numerical Type].format(format) only has the format string, whose sole printf-style "%" argument applies to the value of the number itself.
    - Lang.format(format, parameters) has a completely different kind of format string (with positional arguments $1$, $2$, etc.), and an array of corresponding parameters

    It's unfortunate that the format() method has the same name for both the instance and global versions, which have very different syntax and behaviour.

    Anyway, I whipped up a quick test case for both situations, which seems to work as expected:
    var x = 5;

    var fmt = "%d"; // printf-style format string for Number.format()
    var s1 = x.format("%d");
    var s2 = x.format(fmt);
    System.println("s1 = " + s1);
    System.println("s2 = " + s2);


    var fmt2 = "$1$"; // positional format string for Lang.format()
    var s3 = Lang.format("$1$", [x]);
    var s4 = Lang.format(fmt2, [x]);
    System.println("s3 = " + s3);
    System.println("s4 = " + s4);

    // just to demonstrate that an attempt to call the non-existent String.format will call Lang.format instead
    var s ="abcdefg"; // the value of s really doesn't matter here
    var s5 = s.format("$1$", [x]);
    System.println("s5 = " + s5);


    Output:
    s1 = 5
    s2 = 5
    s3 = 5
    s4 = 5
    s5 = 5
  • Hi. Sorry - let me be more clear. It seems the "fmt" variable, which appears to be proper, should work here.

    What am I doing wrong?


    community.garmin.com/.../1410969.jpg
  • The picture is really tiny and I can't see it, but here's a couple lines out of one of my apps, that does the same basic thing. Here I show a leading zero in hour based on a setting:
    var fmt=(Settings.showZero) ?"%02d":"%d";
    var timeStr=hour.format(fmt);

  • LOL Thx. I see what I did. I assigned "fmt" to include the double quotes by using a backslash. Duh.
  • Glad you found the problem.

    BTW, next time if you need to paste code/text that the forum won't accept, try pastebin.com. It's really handy for pasting code.
  • The (open square bracket) code (close square bracket) tag and a corresponding /code tag works well for code. The code is easy to grab that way and edit it to show a solution.