Search code examples
formattingnumbersthymeleafapostrophe

Thousands separator thymeleaf: use apostrophe


I'm using thymeleaf to render my views. Is it possible to use ' (apostrophe) as a thousands seperator? Example: What I want is to replace WHITESPACE in the following example with a Value for an apostrophe.

<span th:text="${#numbers.formatDecimal(value, 5, 'WHITESPACE', 2, 'POINT' )}"

Is there a NumberPointType for apostrophe or another solution to achieve a formatting like this: 1'000.00


Solution

  • If you dig into the sourcecode of enum org.thymeleaf.util.NumberPointType you will see the only available options with standard #numbers.formatDecimal are..

    public enum NumberPointType {
        POINT("POINT"),
        COMMA("COMMA"),
        WHITESPACE("WHITESPACE"),
        NONE("NONE"),
        DEFAULT("DEFAULT");
    ...
    

    So my preference for you is to create a custom method inside your own code, for example as a bean or a utility object, and create your own function to format the decimal as you wish.

    Update

    public class UIutil {
    
    /**
     * Formats a BigInteger to a thousand grouped String
     * @param number
     * @return
     */
    public static String formatNumber (BigInteger number) {
        return String.format("%,d", number);
    }
    

    }

    Assign:

    context.setVariable("formatter", new UIutil());
    

    Call:

    th:text="${formatter.formatNumber(value)}"