Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsf Convert Number Format

Related Tutorials


 
 

Share on Google+Share on Google+

Convert Number Format

Advertisement
This tutorial discusses about converting a String value in the page to the Number object. When converting, minimum and maximum number of digits in the integer and fractional part can be specified to be formatted.

Convert Number Format

        

This tutorial discusses about converting a String value in the page to the Number object. When converting,  minimum and maximum number of digits in the integer and fractional part can be specified to be formatted. For this we can use convertNumber tag which provides these functionalities. It's attribute "type" specifies the type of formatting like number, currency, percentage.

Code description :

cnf.jsp : In this code we have used convertNumber tag. minFractionDigits specifies minimum no. of digits that is to be formatted. In the same way maxFractionDigits is used to specify maximum no. of digits to be formatted. currencySymbol attribute is used to specify the currency symbol that will be applied when formatting currency values.

<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>

<f:view>
<html>
<head><title>JSF Login Example By Using Component Binding</title></head>

<body>
<h:form>
<table>
   <tr>
  <td><h:outputText value="Enter any number :" /></td>
  <td><h:inputText id="number" >
   <f:convertNumber minFractionDigits="3" maxFractionDigits="5">
   </f:convertNumber>
   </h:inputText>
   <h:message for="number" style="color:red"/></td>
  </tr>
  <tr>
  <td><h:outputText value="Enter currency :" /></td>
  <td><h:inputText id="currency" >
   <f:convertNumber currencySymbol="$" type="currency" >
   </f:convertNumber>
  </h:inputText>
  <h:message for="currency" style="color:red"/></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td><h:commandButton value="Login" /></td>
  </tr>
</table>
</h:form>
</body>
</html>
</f:view>

In this program minFractionDigits is set to "3" and maxFractionDigits to "5" so if we pass less than 3 digits in fractional part then it takes it upto 3 digits and if we pass more than 5 digits then it takes the number upto 5 digits in the fractional part. If the currencySymbol attribute is set to "$" then the value must start with that currency symbol.

Output : This page comes to the user first.

 

If we put the values below :

 

then error message will come indicating the conversion error because "$" sign has not been supplied in the second input text box.

 

If we put more than 5 digits :

 

then it round it off and presents with 5 digits because we have already specified maxFractionDigits to "5".

Download this example : 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 14, 2008

Related Tutorials

Discuss: Convert Number Format  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com