【问题标题】:Thousands separator thymeleaf: use apostrophe千位分隔符 thymeleaf:使用撇号
【发布时间】:2015-04-07 16:12:07
【问题描述】:

我正在使用 thymeleaf 来呈现我的观点。 是否可以使用 ' (撇号)作为千位分隔符? 示例:我想要的是将以下示例中的 WHITESPACE 替换为撇号的值。

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

是否有用于撇号的 NumberPointType 或其他解决方案来实现如下格式:1'000.00

【问题讨论】:

    标签: formatting numbers thymeleaf apostrophe


    【解决方案1】:

    如果您深入研究enumorg.thymeleaf.util.NumberPointType 的源代码,您将看到标准#numbers.formatDecimal 的唯一可用选项是......

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

    所以我对您的偏好是在您自己的代码中创建一个自定义方法,例如 beanutility object,并创建您自己的函数来根据需要格式化小数。

    更新

    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);
    }
    

    }

    分配:

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

    呼叫:

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

    【讨论】:

    • 好的,谢谢。您如何将自定义对象/自定义实现注册到 thymeleaf?
    • 创建一个实用程序bean并像@beanName一样访问或将实用程序对象添加到模型属性中。
    • 好的,谢谢。我将一个 util 对象分配给我调用静态方法的上下文。
    猜你喜欢
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    相关资源
    最近更新 更多