Login  Register

Re: String.format

Posted by Michael Schmid on Nov 18, 2020; 6:55pm
URL: http://imagej.273.s1.nabble.com/String-format-tp5024199p5024209.html

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