【问题标题】:How to use the currency format without the currency symbol or dollar sign如何使用没有货币符号或美元符号的货币格式
【发布时间】:2018-03-29 22:24:54
【问题描述】:

我有:

 ${amount?string.currency} 

它很好地格式化了我的 BigDecimal,唯一的是它包含了我不想要的货币符号(美元符号)。如何在不使用 string["0.##"] 明确指定数字格式的情况下禁用此功能?

【问题讨论】:

    标签: java freemarker


    【解决方案1】:

    目前 (2.3.27) ?string.currency 始终表示 Java 提供的默认货币格式。因此,无需更改它,您可以定义自定义格式并像 amount?string.@currency 一样使用它(其中 currency 只是您为格式指定的名称)。

    自定义格式在 Java 中定义。来自手册 (http://freemarker.org/docs/pgui_config_custom_formats.html#pgui_config_custom_formats_ex_alias):

    // Where you initalize the application-wide Configuration singleton:
    Configuration cfg = ...;
    
    Map<String, TemplateNumberFormatFactory> customNumberFormats = new HashMap<>();
    customNumberFormats.put("price",
            new AliasTemplateNumberFormatFactory(",000.00"));
    customNumberFormats.put("weight",
            new AliasTemplateNumberFormatFactory("0.##;; roundingMode=halfUp"));
    cfg.setCustomNumberFormats(customNumberFormats);
    

    然后在模板中:

    ${product.price?string.@price}
    ${product.weight?string.@weight}
    

    【讨论】:

      【解决方案2】:

      如果你只是想要格式使用

      ${amount?string["0.##"]}
      

      或设置number_format:

      <#setting number_format="0.##">
      

      查看所有 freemarker formats options

      【讨论】:

      • 我可能应该更具体一些,但我基本上是在问如何在没有美元符号的情况下使用 string.currency。您展示的方法将起作用,但我试图让我的用户更简单,因为他们不需要了解有关数字格式的任何内容,只需输入货币即可。这是一个很小的差异,但内部测试表明,有些人更容易理解;)无论如何我都赞成你,因为这是一个正确的答案,而且我的错没有更具体。
      • 我在看,但它是如何在内存中修改 NumberFormat 的实例,它并不真正适用于您无法从模板文件访问实例的 FreeMarker...
      猜你喜欢
      • 2012-01-29
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多