Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

วิธีจัดรูปแบบสกุลเงินใน JSP ด้วย JSTL fmt:formatNumber


แท็ก <fmt:formatNumber> ใช้สำหรับจัดรูปแบบการแสดงผลของ ตัวเลข, เปอร์เซ็นต์ และ สกุลเงิน ใน JSP

แอตทริบิวต์ของ <fmt:formatNumber>

แอตทริบิวต์ คำอธิบาย จำเป็น ค่าเริ่มต้น
value ค่าตัวเลขที่ต้องการจัดรูปแบบ ใช่ None
type ประเภท: NUMBER, CURRENCY, หรือ PERCENT ไม่ Number
pattern รูปแบบการจัดรูปแบบแบบกำหนดเอง ไม่ None
currencyCode รหัสสกุลเงิน (สำหรับ type="currency") ไม่ From the default locale
currencySymbol สัญลักษณ์สกุลเงิน (สำหรับ type="currency") ไม่ From the default locale
groupingUsed ใช้การจัดกลุ่มตัวเลข (true/false) ไม่ true
maxIntegerDigits จำนวนหลักเต็มสูงสุด ไม่ None
minIntegerDigits จำนวนหลักเต็มขั้นต่ำ ไม่ None
maxFractionDigits จำนวนหลักทศนิยมสูงสุด ไม่ None
minFractionDigits จำนวนหลักทศนิยมขั้นต่ำ ไม่ None
var ชื่อตัวแปรเก็บผลลัพธ์ ไม่ Print to page
scope ขอบเขตของตัวแปร ไม่ page

ตัวอย่างการใช้งาน

<%@ taglib prefix="c" uri="https://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="https://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
<title>JSTL fmt:formatNumber Tag - จัดรูปแบบสกุลเงิน</title>
</head>
<body>
<h3>การจัดรูปแบบตัวเลข:</h3>
<c:set var="balance" value="120000.2309" />
<p>สกุลเงิน USA : 
<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/>
</p>
</body>
</html>

ผลลัพธ์

การจัดรูปแบบตัวเลข:
สกุลเงิน USA : $120,000.23

หมายเหตุ: การใช้ <fmt:setLocale> จะส่งผลต่อรูปแบบสกุลเงิน ตัวคั่นหลักพัน และสัญลักษณ์สกุลเงิน