如何在JSP中格式化数字?
<fmt:formatNumber>标签用于格式化数字、百分比和货币。
属性
<fmt:formatNumber>标签具有以下属性:
| 属性 | 描述 | 必填 | 默认值 |
|---|---|---|---|
| 值 | 要显示的数值 | 是 | 无 |
| 类型 (type) | NUMBER、CURRENCY 或 PERCENT | 否 | 数字 (Number) |
| 模式 (pattern) | 指定输出的自定义格式模式。 | 否 | 无 |
| 货币代码 (currencyCode) | 货币代码(对于 type = "currency") | 否 | 来自默认区域设置 |
| 货币符号 (currencySymbol) | 货币符号(对于 type = "currency") | 否 | 来自默认区域设置 |
| 是否使用分组 (groupingUsed) | 是否对数字进行分组 (TRUE 或 FALSE) | 否 | true |
| 最大整数位数 (maxIntegerDigits) | 要打印的最大整数位数 | 否 | 无 |
| 最小整数位数 (minIntegerDigits) | 要打印的最小整数位数 | 否 | 无 |
| 最大小数位数 (maxFractionDigits) | 要打印的最大小数位数 | 否 | 无 |
| 最小小数位数 (minFractionDigits) | 要打印的最小小数位数 | 否 | 无 |
| 变量 (var) | 用于存储格式化数字的变量名称 | 否 | 打印到页面 |
| 范围 (scope) | 用于存储格式化数字的变量范围 | 否 | 页面 (page) |
示例
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL fmt:formatNumber Tag</title>
</head>
<body>
<h3>Number Format:</h3>
<c:set var = "balance" value = "120000.2309" />
<p>Formatted Number (1): <fmt:formatNumber value = "${balance}" type = "currency"/></p>
<p>Formatted Number (2): <fmt:formatNumber type = "number" maxIntegerDigits = "3" value = "${balance}" /></p>
<p>Formatted Number (3): <fmt:formatNumber type = "number" maxFractionDigits = "3" value = "${balance}" /></p>
<p>Formatted Number (4): <fmt:formatNumber type = "number" groupingUsed = "false" value = "${balance}" /></p>
</body>
</html>以上代码将生成以下结果:
Number Format: Formatted Number (1): £120,000.23 Formatted Number (2): 000.231 Formatted Number (3): 120,000.231 Formatted Number (4): 120000.231
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP