2

I would like to convert an integer into a string with leading zeros in ImageJ / Fiji macro editor using String.format() function. With this code:

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

I get the following error:

Unrecognized String function in line 2
s = String . <format> ("%04d" , 13);

Any ideas?

After the documentation https://imagej.nih.gov/ij/developer/macro/functions.html#S this should work:

String.format(format, number) - Returns a formatted string using the specified format and number.

Version: ImageJ 1.53c with Java 1.8.0_172 (64-bit)

NicoH
  • 1,240
  • 3
  • 12
  • 23
  • I get a different error on `(Fiji Is Just) ImageJ 2.1.0/1.53i; Java 1.8.0_172 [64-bit]`. It looks like a bug to me. You could use `String.pad(n, length)` as an alternative. It will add leading zeros. – quantixed May 20 '21 at 16:19
  • Thanks, unfortunately `String.pad()` throws the same error for `pad`. I ended up using the following workaround: `s = toString(13); while (s.length() < 4) {s = "0" + s;}` – NicoH May 21 '21 at 07:41
  • Glad it's solved. Try updating ImageJ. There's something in the release notes about String.pad in 1.53g – quantixed May 22 '21 at 07:45

1 Answers1

2

The String.format macro function requires ImageJ 1.53d or newer. The version of ImageJ 1.x shipped with Fiji is a few versions behind due to breaking changes in ImageJ 1.x that the ImageJ2 team still need to work around. In the meantime, you can use the Help > Update ImageJ... command to update the version of ImageJ 1.x bundled with Fiji. Be aware that this command will hard quit your running ImageJ instance after downloading the chosen version.

ctrueden
  • 6,751
  • 3
  • 37
  • 69