Java Tutorial/JSTL/Format Number — различия между версиями

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

Текущая версия на 15:24, 31 мая 2010

Format Number for Percent Max Fraction Digits

<%@ 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>
   <fmt:formatNumber type="percent" maxFractionDigits="3"
            value="123123.123123" />
  </body>
</html>





Format Number: For Percent Max Integer Digits

<%@ 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>
   <fmt:formatNumber type="percent" maxIntegerDigits="3"
            value="123123.123123" />
  </body>
</html>





Format Number: For Percent Min Fraction Digits

<%@ 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>
   <fmt:formatNumber type="percent" minFractionDigits="3"
            value="123123.123123" />
  </body>
</html>





Format Number: For Percent Min Integer Digits

<%@ 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>
   <fmt:formatNumber type="percent" minIntegerDigits="3"
            value="123123.123123" />
  </body>
</html>





Format Number: Max Fraction Digits

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>
  <body>
  <fmt:formatNumber type="number" maxFractionDigits="3" value="123456789.123456789" />
  <br><br>
  <fmt:formatNumber type="number" maxFractionDigits="3" value="9.123456789" />
  <br><br>
  <fmt:formatNumber type="number" maxFractionDigits="3" value="89.1" />
  </body>
</html>





Format Number Max Integer Digits

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>
  <body>
  <fmt:formatNumber type="number" maxIntegerDigits="3" value="123456789.123456789" />
  <br><br>
  <fmt:formatNumber type="number" maxIntegerDigits="3" value="9.123456789" />
  <br><br>
  <fmt:formatNumber type="number" maxIntegerDigits="3" value="89.1" />
  </body>
</html>





Format Number Min Fraction Digits

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>
  <body>
  <fmt:formatNumber type="number" minFractionDigits="3" value="123456789.123456789" />
  <br><br>
  <fmt:formatNumber type="number" minFractionDigits="3" value="9.123456789" />
  <br><br>
  <fmt:formatNumber type="number" minFractionDigits="3" value="89.1" />
  </body>
</html>





Format Number Min Integer Digits

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>
  <body>
  <fmt:formatNumber type="number" minIntegerDigits="3" value="123456789.123456789" />
  <br><br>
  <fmt:formatNumber type="number" minIntegerDigits="3" value="9.123456789" />
  <br><br>
  <fmt:formatNumber type="number" minIntegerDigits="3" value="89.1" />
  </body>
</html>





Format Number with default format

<%@ taglib uri="http://java.sun.ru/jstl/core" prefix="c" %><%@ taglib uri="http://java.sun.ru/jstl/core-rt" prefix="c-rt" %><%@ taglib uri="http://java.sun.ru/jstl/fmt" prefix="fmt" %>
<html>
  <head>
    <title>Format Date</title>
  </head>
  <body>
    <c-rt:set var="now" value="<%=new java.util.Date()%>" />
    <fmt:formatNumber type="number" value="123.123" />
  </body>
</html>





Format percent number from form input

<%@ 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>
        <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>





Use JSTL to Format Number From Form

<%@ 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>
        <tr>
          <td width="100%" colspan="2" bgcolor="#0000FF">
                Formatting
          </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>
        <tr><td width="100%" colspan="2" Formatting: <c:out value="${param.num}" />
          </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>