Java/JSTL/Number

Материал из Java эксперт
Перейти к: навигация, поиск

JSTL format: number

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Number</title>
  </head>
  <body>
    <form method="POST">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="62%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Number
                Formatting</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="47%">Enter a number to be formatted:</td>
          <td width="53%">
            <input type="text" name="num" size="20" />
          </td>
        </tr>
        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="submit" />
              <input type="reset" value="Reset" name="reset" />
            </p>
          </td>
        </tr>
      </table>
      <p>&#160;</p>
    </form>
    <c:if test="${pageContext.request.method=="POST"}">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="63%" id="AutoNumber2">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Formatting: 
                <c:out value="${param.num}" />
                </font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="51%">type="number"</td>
          <td width="49%">
            <fmt:formatNumber type="number" value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="number" maxIntegerDigits="3"</td>
          <td>
            <fmt:formatNumber type="number" maxIntegerDigits="3"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="number" minIntegerDigits="10"</td>
          <td>
            <fmt:formatNumber type="number" minIntegerDigits="10"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="number" maxFractionDigits="3"</td>
          <td>
            <fmt:formatNumber type="number" maxFractionDigits="3"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="number" minFractionDigits="10"</td>
          <td>
            <fmt:formatNumber type="number" minFractionDigits="10"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="number" maxFractionDigits="3"
          groupingUsed="false"</td>
          <td>
            <fmt:formatNumber type="number" maxFractionDigits="3"
            groupingUsed="false" value="${param.num}" />
          </td>
        </tr>
      </table>
    </c:if>
  </body>
</html>





JSTL: Format Percent

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Percent</title>
  </head>
  <body>
    <form method="POST">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="62%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Number
                Formatting</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="47%">Enter a percent to be formatted:</td>
          <td width="53%">
            <input type="text" name="num" size="20" />
          </td>
        </tr>
        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="submit" />
              <input type="reset" value="Reset" name="reset" />
            </p>
          </td>
        </tr>
      </table>
      <p>&#160;</p>
    </form>
    <c:if test="${pageContext.request.method=="POST"}">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="63%" id="AutoNumber2">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Formatting: 
                <c:out value="${param.num}" />
                </font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="51%">type="percent"</td>
          <td width="49%">
            <fmt:formatNumber type="percent"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="percent" maxIntegerDigits="3"</td>
          <td>
            <fmt:formatNumber type="percent" maxIntegerDigits="3"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="percent" minIntegerDigits="10"</td>
          <td>
            <fmt:formatNumber type="percent" minIntegerDigits="10"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="percent" maxFractionDigits="3"</td>
          <td>
            <fmt:formatNumber type="percent" maxFractionDigits="3"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="percent" minFractionDigits="10"</td>
          <td>
            <fmt:formatNumber type="percent" minFractionDigits="10"
            value="${param.num}" />
          </td>
        </tr>
        <tr>
          <td>type="percent" maxFractionDigits="3"
          groupingUsed="false"</td>
          <td>
            <fmt:formatNumber type="percent" maxFractionDigits="3"
            groupingUsed="false" value="${param.num}" />
          </td>
        </tr>
      </table>
    </c:if>
  </body>
</html>





JSTL Parse Number

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Parse Number</title>
  </head>
  <body>
    <form method="POST">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="62%" id="AutoNumber1">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Number
                Formatting</font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="47%">Enter a number to be parsed:</td>
          <td width="53%">
            <input type="text" name="num" size="20" />
          </td>
        </tr>
        <tr>
          <td width="100%" colspan="2">
            <p align="center">
              <input type="submit" value="Submit" name="submit" />
              <input type="reset" value="Reset" name="reset" />
            </p>
          </td>
        </tr>
      </table>
      <p>&#160;</p>
    </form>
    <c:if test="${pageContext.request.method=="POST"}">
      <table border="1" cellpadding="0" cellspacing="0"
      style="border-collapse: collapse" bordercolor="#111111"
      width="63%" id="AutoNumber2">
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
            <p align="center">
              <b>
                <font color="#FFFFFF" size="4">Formatting: 
                <c:out value="${param.num}" escapeXml="false" />
                </font>
              </b>
            </p>
          </td>
        </tr>
        <tr>
          <td width="51%">type="number"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" type="number"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
        <tr>
          <td width="51%">type="currency"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" type="currency"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
        <tr>
          <td width="51%">type="percent"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" type="percent"
              value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
        <tr>
          <td width="51%">type="number" integerOnly="true"</td>
          <td width="49%">
            <c:catch var="e">
              <fmt:parseNumber var="i" integerOnly="true"
              type="number" value="${param.num}" />
              <c:out value="${i}"  escapeXml="false" />
            </c:catch>
            <c:out value="${e}"  escapeXml="false" />
          </td>
        </tr>
      </table>
    </c:if>
  </body>
</html>