fmt:formatNumber use locale display negative currency in -$xxx.xx format in JSTL

本文介绍如何使用JSTL中的fmt:formatNumber标签来格式化数字为特定地区的货币格式,并提供了在美国和德国风格下显示的例子。同时展示了如何自定义模式以改变负数的显示方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

First, we want to know our own locale,how to display the locale in a JSTL?

<c:out value="${pageContext.request.locale.language}"/> 


I'm confused with the jstl tag libs:

I want to format a number to a currency with german style

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<html>
<head>
    <title>format number</title>
</head>
<body>
    <c:set var="val" value="40.52" />
    <p> Currency in USA
    <fmt:setLocale value="en_US" scope="session"/>
    <fmt:formatNumber value="${val}" type="currency" />
    </p>
    <p>Currency in Germany
    <fmt:setLocale value="de_DE" scope="session"/>
    <fmt:formatNumber value="${val}" type="currency"/>
    </p>
</body>
</html>

And thats the output:
Currency in USA $40.52
Currency in Germany 40,52 



I am using fmt:formatNumber to format currency in JSTL, it will display negative currency in ($100) format, if you want to make it display negative currency in negative format instead of ($100).

I would suggest:

<fmt:formatNumber type="currency" pattern="$#,##0.00;-$#,##0.00" value="-10000" />

You can remove '$' from the pattern, if you like.


order example:

<c:set  var="val"  value="46563746375"/>
<fmt:formatNumber  value="${val}"  pattern="###.###E0"/>

If you run the above code you will see the number is formatted as the pattern which is 46.5637E9 .



转载于:https://www.cnblogs.com/molashaonian/p/7242022.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值