Format percent number from form input : Format Number « JSTL « Java Tutorial






<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.com/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Percent</title>
  </head>

  <body>
    <form method="POST">
      <table>
        <tr>
          <td width="100%" colspan="2">
                Number Formatting
          </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>
        <tr>
          <td width="100%" colspan="2">
                Formatting: <c:out value="${param.num}"/>
          </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>
  Download:  JSTLFormatNumberPercentFormData.zip( 1,226 k)








24.20.Format Number
24.20.1.Format Number with default format
24.20.2.Format percent number from form input
24.20.3.Format Number Min Integer Digits
24.20.4.Format Number Min Fraction Digits
24.20.5.Format Number Max Integer Digits
24.20.6.Format Number: Max Fraction Digits
24.20.7.Use JSTL to Format Number From Form
24.20.8.Format Number: For Percent Min Integer Digits
24.20.9.Format Number: For Percent Min Fraction Digits
24.20.10.Format Number: For Percent Max Integer Digits
24.20.11.Format Number for Percent Max Fraction Digits