我是Grails(2.2.0)的新手,并尝试弄清楚如何在视图中格式化双值。我有很多它们,因此如果可能的话,我希望在其中一个配置文件中使用一些代码。
视图中的所有值都使用g:field ... type =“number”...
进行格式化在resources.groovy我有这个
beans = {
localeResolver(SessionLocaleResolver) {
defaultLocale= Locale.ENGLISH
Locale.setDefault (Locale.ENGLISH)
}
}
在messages.properties中,我使用了
default.number.format=#'###'###.#
我不需要i18n。我删除了那里的所有其他文件。
此问题仅在视图中出现。所有数据都保持正确。
这里有一些我想要的视图示例:
输入10 输出10
输入0 输出0
输入0.775 输出0.775
输入0.7 输出0.7
输入1234567.12345 输出1'234'567.12345
最后一个例子不是ENGLISH的标准格式。而不是一千个分隔符,我想要'
我想如果我用
default.number.format=#'###'###.#
我不需要用g:formatNumber格式化每个值。
我感谢任何帮助。提前谢谢!
修改
以下是我的问题:
为什么default.number.format =#'###'###。##在messages.properties中不起作用?
例如我在我看来有这个:
<g:field name="mydouble" size="8" maxlength="20" type="number" value="${fieldValue(bean: myInstance, field: 'mydouble')}"/>
如何在此字段上使用g:formatNumber标记。我已经阅读了Grails文档,但显然我在理解它时遇到了问题。
我写道,我是初学者,因此我可能会遗漏那些了解Grails非常明显的人。
答案 0 :(得分:1)
我不确定我是否理解这个问题,但请看看这是否适合你:
<g:field value="${g.formatNumber(number: fieldValue(bean: personInstance, field: 'minPrice'), format: g.message(code: 'default.number.format'))}" />
答案 1 :(得分:0)
在GSP中渲染数字时尝试使用fieldValue
。
<g:field value="${fieldValue(bean: personInstance, field: 'minPrice')}" />
不确定是否存在使用默认值的问题,因为您遗漏了问题中的一些细节。所以在这里黑暗中拍摄。
答案 2 :(得分:0)
那么,您需要使用'
作为小数点分隔符吗?尝试这样的格式化方法:
class NumberUtilities {
static String format(double d) {
def format = new DecimalFormat()
def symbols = format.decimalFormatSymbols
symbols.groupingSeparator = '\''
format.decimalFormatSymbols = symbols
return format.format(d)
}
}
然后在您的视图中写下${NumberUtilities.format(1234567.12345)}
。
这只是一个想法,根据您需要的其他格式规则为方法添加更多格式。