2

All is say in the title. I want to transform double in String with 2 decimals.

I tried with the example of quarkus :

@TemplateExtension(namespace = "str")
class StringExtensions {

   static String format(String fmt, Object... args) {
      return String.format(fmt, args);
   }

   static String reverse(String val) {
      return new StringBuilder(val).reverse().toString();
   }
}

And is my template

{str:format('%.2f', total)}

But It always results in NOT_FOUND.

The only way is putting :

    static String formatNumber(Double num) {
        return String.format("%.2f", num);
    }

and change my template :

{total.formatNumber}

Is there a better way ? I used the quarkus-rest-easy-qute et quarkus-qute extension both is the same.

Dorian Maliszewski
  • 821
  • 1
  • 10
  • 14

1 Answers1

1

there was a bug in how varargs were handled for extension methods with a namespace. I've just created A PR with a fix: https://github.com/quarkusio/quarkus/pull/14226.

An ugly workaround is to use an additional "dummy" param in the template, e.g. {str:format('%.2f', total, 1)}, and ignore this param in the specified format.

Martin Kouba
  • 1,121
  • 5
  • 8