String.format

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

String.format

Peter Haub
Hi,
I would like to execute the macro function:

s = String.format("%04d", 13);
print(s);

but I get the error message
"java.util.IllegalFormatConversionException: d != java.lang.Double in
line 1:"
"s = String . format ( "%04d" , 13 <)> ;"

If the number is float and not integer It's working:

s = String.format("%04f", 13.0);
print(s);

Could this be a bug?

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Kenneth R Sloan-2
I suspect that all numbers are doubles in macros.
--
Kenneth Sloan
[hidden email]
Vision is the art of seeing what is invisible to others.

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Wayne Rasband-2
In reply to this post by Peter Haub
> On Nov 18, 2020, at 10:53 AM, Peter Haub <[hidden email]> wrote:
>
> Hi,
> I would like to execute the macro function:
>
> s = String.format("%04d", 13);
> print(s);
>
> but I get the error message

Change the ‘d’ (integer) to ‘f’ (float) and it will work. All numbers in the ImageJ macro language are stored as doubles.

-wayne


> "java.util.IllegalFormatConversionException: d != java.lang.Double in
> line 1:"
> "s = String . format ( "%04d" , 13 <)> ;"
>
> If the number is float and not integer It's working:
>
> s = String.format("%04f", 13.0);
> print(s);
>
> Could this be a bug?
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Peter Haub
In reply to this post by Kenneth R Sloan-2
Thanks Kenneth,

but this issue could be solved by changing the current implementation in
Functions.java  doString()

from
         else if (name.equals("format")) {
             try {return String.format(getFirstString(), getLastArg());}
             catch (Exception e) {interp.error(""+e);}
             return null;
         }
         ...

to
         ...
         else if (name.equals("format")) {
             String formatStr = getFirstString();
             double num = getLastArg();
             try {
                 if (formatStr.contains("d"))
                     return String.format(formatStr, (int) num);
                 else
                     return String.format(formatStr, num);}
             catch (Exception e) {interp.error(""+e);}
             return null;
         }
         ...

Here is a use case
https://forum.image.sc/t/fiji-macro-issue-no-window-with-title-found/45525/3

Peter


On 18.11.2020 17:04, Kenneth Sloan wrote:
> I suspect that all numbers are doubles in macros.
> --
> Kenneth Sloan
> [hidden email]
> Vision is the art of seeing what is invisible to others.
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Wayne Rasband-2
> On Nov 18, 2020, at 11:49 AM, Peter Haub <[hidden email]> wrote:
>
> Here is a use case
> https://forum.image.sc/t/fiji-macro-issue-no-window-with-title-found/45525/3

The easy way to add leading zeros to an integer is to use the IJ.pad(n,digits) function. It is not obvious how to do it using String.format().

-wayne

> On 18.11.2020 17:04, Kenneth Sloan wrote:
>> I suspect that all numbers are doubles in macros.
>> --
>> Kenneth Sloan
>> [hidden email]
>> Vision is the art of seeing what is invisible to others.
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Peter Haub
Thank you Wayne
for this valuable hint.
Perfect solution.

Peter

On 18.11.2020 18:24, Wayne Rasband wrote:

>> On Nov 18, 2020, at 11:49 AM, Peter Haub <[hidden email]> wrote:
>>
>> Here is a use case
>> https://forum.image.sc/t/fiji-macro-issue-no-window-with-title-found/45525/3
> The easy way to add leading zeros to an integer is to use the IJ.pad(n,digits) function. It is not obvious how to do it using String.format().
>
> -wayne
>
>> On 18.11.2020 17:04, Kenneth Sloan wrote:
>>> I suspect that all numbers are doubles in macros.
>>> --
>>> Kenneth Sloan
>>> [hidden email]
>>> Vision is the art of seeing what is invisible to others.
>>>
>>> --
>>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Michael Schmid
In reply to this post by Peter Haub
Hi Peter,

sorry, one can't replace all 'd' characters in the format string with
'f'. The format string can also contain text, e.g.
   String.format("did you know: pi=%f", 3.14159);
prints
   did you know: pi=3.141590

The 'd's of "did" would be also replaced.

One might replace all "%d", but then one should also care about "%+9f"
(uses a minimum of 9 characters and always prints the sign) etc.
It might be possible to find a regular expression for replacing all the
variants (keeping the flags and width, and resulting in
"%<flags><width>f.0"), but it is not so easy.

Michael
________________________________________________________________
On 18.11.20 17:49, Peter Haub wrote:

> Thanks Kenneth,
>
> but this issue could be solved by changing the current implementation in
> Functions.java  doString()
>
> from
>          else if (name.equals("format")) {
>              try {return String.format(getFirstString(), getLastArg());}
>              catch (Exception e) {interp.error(""+e);}
>              return null;
>          }
>          ...
>
> to
>          ...
>          else if (name.equals("format")) {
>              String formatStr = getFirstString();
>              double num = getLastArg();
>              try {
>                  if (formatStr.contains("d"))
>                      return String.format(formatStr, (int) num);
>                  else
>                      return String.format(formatStr, num);}
>              catch (Exception e) {interp.error(""+e);}
>              return null;
>          }
>          ...
>
> Here is a use case
> https://forum.image.sc/t/fiji-macro-issue-no-window-with-title-found/45525/3 
>
>
> Peter
>
>
> On 18.11.2020 17:04, Kenneth Sloan wrote:
>> I suspect that all numbers are doubles in macros.
>> --
>> Kenneth Sloan
>> [hidden email]
>> Vision is the art of seeing what is invisible to others.
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Stein Rørvik
In reply to this post by Peter Haub
Peter,

If what you want to do is to zero pad an integer,
you can do this by formatting it as a float specifying zero decimals:

        s = String.format("%04.0f", 13);
        print(s);

prints 0013

        str = String.format("%09.5f", PI);
        print(str);

prints 003.14159

The last syntax is useful as it allows for more complex number formatting.
You can for example pad with spaces instead:
        str = String.format("% 9.5f", PI);

Apparently, String.format does not work as expected with numbers if you omit the .n decimal specification in the format string.

The IJ.pad() function is easier to use, but only works with integers.

Stein

-----Original Message-----
Sent: 18. november 2020 16:54
Subject: String.format

Hi,
I would like to execute the macro function:

s = String.format("%04d", 13);
print(s);

but I get the error message
"java.util.IllegalFormatConversionException: d != java.lang.Double in line 1:"
"s = String . format ( "%04d" , 13 <)> ;"

If the number is float and not integer It's working:

s = String.format("%04f", 13.0);
print(s);

Could this be a bug?

--
ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no%7Ce5f8a69be3104cb30bda08d88bda59ba%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C1%7C637413117218634903%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=Zn5pKbxK4MklXs3rgwo1Xjs4RtWJ9OBnnD7iezL%2BIZQ%3D&amp;reserved=0

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Herbie
In case it matters, the following macro function (that works similarly
in Java) does it in a handcrafted fashion:

///////
requires("1.53f");
string = preZeros( d2s( PI, 5 ), 3 );
print( string );

function preZeros( inStr, n ){
    if ( inStr.contains( "." ) ) {
       str = split( inStr, "[.]" );
       inStr = str[0];
       dec = "." + str[1];
    }
    while ( inStr.length() < n ) inStr = "0" + inStr;
    return inStr + dec;
}
//////

Have fun

Herbie

::::::::::::::::::::::::::::::::::::::::::
Am 21.11.20 um 13:33 schrieb Stein Rørvik:

> Peter,
>
> If what you want to do is to zero pad an integer,
> you can do this by formatting it as a float specifying zero decimals:
>
> s = String.format("%04.0f", 13);
> print(s);
>
> prints 0013
>
> str = String.format("%09.5f", PI);
> print(str);
>
> prints 003.14159
>
> The last syntax is useful as it allows for more complex number formatting.
> You can for example pad with spaces instead:
> str = String.format("% 9.5f", PI);
>
> Apparently, String.format does not work as expected with numbers if you omit the .n decimal specification in the format string.
>
> The IJ.pad() function is easier to use, but only works with integers.
>
> Stein
>
> -----Original Message-----
> Sent: 18. november 2020 16:54
> Subject: String.format
>
> Hi,
> I would like to execute the macro function:
>
> s = String.format("%04d", 13);
> print(s);
>
> but I get the error message
> "java.util.IllegalFormatConversionException: d != java.lang.Double in line 1:"
> "s = String . format ( "%04d" , 13 <)> ;"
>
> If the number is float and not integer It's working:
>
> s = String.format("%04f", 13.0);
> print(s);
>
> Could this be a bug?
>
> --
> ImageJ mailing list: https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no%7Ce5f8a69be3104cb30bda08d88bda59ba%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C1%7C637413117218634903%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=Zn5pKbxK4MklXs3rgwo1Xjs4RtWJ9OBnnD7iezL%2BIZQ%3D&amp;reserved=0
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: String.format

Peter Haub
Dear Stein,
Dear Herbie,

Thanks for your helpful and useful suggestions
+++ :-)

Peter

ps: Herbie, you are absolutely right ...
Commands like

run("Find Maxima...", "noise=&noise output=&output light");

are absolutely correct
(according to https://imagej.nih.gov/ij/developer/macro/macros.html)

On 21.11.2020 14:09, Herbie wrote:

> In case it matters, the following macro function (that works similarly
> in Java) does it in a handcrafted fashion:
>
> ///////
> requires("1.53f");
> string = preZeros( d2s( PI, 5 ), 3 );
> print( string );
>
> function preZeros( inStr, n ){
>    if ( inStr.contains( "." ) ) {
>       str = split( inStr, "[.]" );
>       inStr = str[0];
>       dec = "." + str[1];
>    }
>    while ( inStr.length() < n ) inStr = "0" + inStr;
>    return inStr + dec;
> }
> //////
>
> Have fun
>
> Herbie
>
> ::::::::::::::::::::::::::::::::::::::::::
> Am 21.11.20 um 13:33 schrieb Stein Rørvik:
>> Peter,
>>
>> If what you want to do is to zero pad an integer,
>> you can do this by formatting it as a float specifying zero decimals:
>>
>>     s = String.format("%04.0f", 13);
>>     print(s);
>>
>> prints 0013
>>
>>     str = String.format("%09.5f", PI);
>>     print(str);
>>
>> prints 003.14159
>>
>> The last syntax is useful as it allows for more complex number
>> formatting.
>> You can for example pad with spaces instead:
>>     str = String.format("% 9.5f", PI);
>>
>> Apparently, String.format does not work as expected with numbers if
>> you omit the .n decimal specification in the format string.
>>
>> The IJ.pad() function is easier to use, but only works with integers.
>>
>> Stein
>>
>> -----Original Message-----
>> Sent: 18. november 2020 16:54
>> Subject: String.format
>>
>> Hi,
>> I would like to execute the macro function:
>>
>> s = String.format("%04d", 13);
>> print(s);
>>
>> but I get the error message
>> "java.util.IllegalFormatConversionException: d != java.lang.Double in
>> line 1:"
>> "s = String . format ( "%04d" , 13 <)> ;"
>>
>> If the number is float and not integer It's working:
>>
>> s = String.format("%04f", 13.0);
>> print(s);
>>
>> Could this be a bug?
>>
>> --
>> ImageJ mailing list:
>> https://eur03.safelinks.protection.outlook.com/?url=http%3A%2F%2Fimagej.nih.gov%2Fij%2Flist.html&amp;data=04%7C01%7Cstein.rorvik%40sintef.no%7Ce5f8a69be3104cb30bda08d88bda59ba%7Ce1f00f39604145b0b309e0210d8b32af%7C1%7C1%7C637413117218634903%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&amp;sdata=Zn5pKbxK4MklXs3rgwo1Xjs4RtWJ9OBnnD7iezL%2BIZQ%3D&amp;reserved=0
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html


--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html