Number.format() documentation

Former Member
Former Member
Hi,

some documentation on the supported formatting specifiers in Number.format(format) would be useful.

I tried using for example "%.2f" to get a two-digit-after-the-dot decimal, but that doesn't seem to be supported.

Cheers,
Matt
  • I've used it in test code and it seemed to work correctly. For example, given the following code

    var f = 3.1451234;
    Sys.println("%%.2f='" + f.format("%.2f") + "'");


    I get this...

    %.2f='3.15'


    Given a little bit of testing, it seems that the a,A,g,G,e,E specifiers aren't supported. It does appear that the flags, width, and precision specifications are supported for each of the supported specifiers though.

    %.2d='03'
    %+.2d='+03'
    %#.2d='03'
    %0.2d='03'
    %0.2d='03'
    %10.2d=' 03'
    % 10.2d=' 03'
    %+10.2d=' +03'
    % +10.2d=' +03'
    %-10.2d='03 '
    % -10.2d=' 03 '
    %#10.2d=' 03'
    %010.2d=' 03'
    %010.2d=' 03'
    %.2x='03'
    %+.2x='03'
    %#.2x='0x03'
    %0.2x='03'
    %0.2x='03'
    %10.2x=' 03'
    % 10.2x=' 03'
    %+10.2x=' 03'
    % +10.2x=' 03'
    %-10.2x='03 '
    % -10.2x='03 '
    %#10.2x=' 0x03'
    %010.2x=' 03'
    %010.2x=' 03'
    %.2u='03'
    %+.2u='03'
    %#.2u='03'
    %0.2u='03'
    %0.2u='03'
    %10.2u=' 03'
    % 10.2u=' 03'
    %+10.2u=' 03'
    % +10.2u=' 03'
    %-10.2u='03 '
    % -10.2u='03 '
    %#10.2u=' 03'
    %010.2u=' 03'
    %010.2u=' 03'
    %.2o='03'
    %+.2o='03'
    %#.2o='03'
    %0.2o='03'
    %0.2o='03'
    %10.2o=' 03'
    % 10.2o=' 03'
    %+10.2o=' 03'
    % +10.2o=' 03'
    %-10.2o='03 '
    % -10.2o='03 '
    %#10.2o=' 03'
    %010.2o=' 03'
    %010.2o=' 03'
    %.2f='3.14'
    %+.2f='+3.14'
    %#.2f='3.14'
    %0.2f='3.14'
    %0.2f='3.14'
    %10.2f=' 3.14'
    % 10.2f=' 3.14'
    %+10.2f=' +3.14'
    % +10.2f=' +3.14'
    %-10.2f='3.14 '
    % -10.2f=' 3.14 '
    %#10.2f=' 3.14'
    %010.2f='0000003.14'
    %010.2f='0000003.14'


    That said, this stuff really should have some documentation. I've noticed that Sys.println() (and friends) seem to treat % as the start of a format specification, and that causes bad behavior if you don't escape it as %%.
  • Former Member
    Former Member over 10 years ago
    Thanks for your reply & tests.

    Of course you're right, precisions are supported. Turns out I got confused somewhere when converting numbers to strings.
  • It's been a while, but since this was the first Google result for my search, this is documented now: developer.garmin.com/.../Number.html