Java JSTL数字格式:如何构造此模式?

Java JSTL数字格式:如何构造此模式?,java,jsp,formatting,jstl,decimal,Java,Jsp,Formatting,Jstl,Decimal,我试图在JSTL中创建一个数字格式模式: <fmt:message var="myPattern" key="" /> 简单地说:小数指示符是逗号。分组指示符是点。 setLocale解决方案对我不起作用。解决方案必须完全基于模式。 (可控性问题) 假设模式将直接从数据库中导出 您可以使用fn:substring功能实现它 <c:set value="123456789.00" var="phone"/> <c:out value="${fn:substring

我试图在JSTL中创建一个数字格式模式:

<fmt:message var="myPattern" key="" />
  • 简单地说:小数指示符是逗号。分组指示符是点。
  • setLocale解决方案对我不起作用。解决方案必须完全基于模式。 (可控性问题)
  • 假设模式将直接从数据库中导出

您可以使用
fn:substring
功能实现它

<c:set value="123456789.00" var="phone"/>
<c:out value="${fn:substring(phone, 0, 3)}.${fn:substring(phone,3,6)}.${fn:substring(phone,6,9)},${fn:substring(phone,10,12)}"/>

别忘了加上

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>


贴上标签

我想你不知道数字的长度。因此,您可以使用默认的数字模式:

<c:set var="output"><fmt:formatNumber type="number" maxFractionDigits="2" value="${yournumber}"/></c:set>

现在输出的格式为:123456789.00

接下来使用fn:replace将句点替换为逗号(使用@作为占位符)


或作为一项声明:

<c:set var="output" value="${fn:replace(fn:replace(fn:replace(output, ".", "@"), ",", "."), "@", ",")}"/>


注意:我没有测试这些行以确保它们没有拼写错误,因此如果您尝试了这些行并且必须修复它们,请返回报告。

尝试了此模式
pattern=“##########,##”
?@SanKrish-不起作用。错误:java.lang.IllegalArgumentException:模式“#############,###”中的多个十进制分隔符是什么意思?你是如何输出这些数字的?@ink.robot我的答案能帮你吗。如果您还有任何问题,请在我的回答下面发表评论
<c:set var="output" value="${fn:replace(output, ".", "@")}"/>
<c:set var="output" value="${fn:replace(output, ",", ".")}"/>
<c:set var="output" value="${fn:replace(output, "@", ",")}"/>
<c:set var="output" value="${fn:replace(fn:replace(fn:replace(output, ".", "@"), ",", "."), "@", ",")}"/>