问题
I have data coming into JSP from database. this has teh value like 0000000019 (string). I need to display it as 19. leading zeros needs to trimed out.how would you do it in jsp?
Update
see I have a session variable as :
<c:set var = "tCharge" value="${redata.totalCharge}" />
how do you trm all the leading zeros from "tCharge" or ${redata.totalCharge}
回答1:
You can use <fmt:formatNumber value="${redata.totalCharge}"/>
回答2:
Always zeros? Converting it to number should do it.
<%
String sNum="0000000019";
int iNum = Integer.parseInt(sNum);
sNum = Integer.toString(iNum);
%>
回答3:
is the data an integer number? the fastest way is
<%=Integer.parseInt("0000000019")%>
or if it comes from the session...
<%=Integer.parseInt(((YourRedataClass)session.getAttribute("redata")).getTotalCharge())%>
回答4:
fmt:formatNumber
didn't work for me. But fmt:parseNumber
works:
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:parseNumber value="0000000123456" />
来源:https://stackoverflow.com/questions/4786966/how-to-trim-leading-zeros-before-displaying-the-field-in-jsp