org.springframework.format.number
Class CurrencyFormatter

java.lang.Object
  extended by org.springframework.format.number.AbstractNumberFormatter
      extended by org.springframework.format.number.CurrencyFormatter
All Implemented Interfaces:
Formatter<java.lang.Number>, Parser<java.lang.Number>, Printer<java.lang.Number>

public class CurrencyFormatter
extends AbstractNumberFormatter

A BigDecimal formatter for currency values.

Delegates to NumberFormat.getCurrencyInstance(Locale). Configures BigDecimal parsing so there is no loss of precision. Can apply a specified RoundingMode to parsed values.

Since:
3.0
Author:
Keith Donald, Juergen Hoeller
See Also:
AbstractNumberFormatter.setLenient(boolean), setRoundingMode(java.math.RoundingMode)

Field Summary
private  java.util.Currency currency
           
private  int fractionDigits
           
private  java.math.RoundingMode roundingMode
           
private static boolean roundingModeOnDecimalFormat
           
 
Constructor Summary
CurrencyFormatter()
           
 
Method Summary
protected  java.text.NumberFormat getNumberFormat(java.util.Locale locale)
          Obtain a concrete NumberFormat for the specified locale.
 java.math.BigDecimal parse(java.lang.String text, java.util.Locale locale)
          Parse a text String to produce a T.
 void setCurrency(java.util.Currency currency)
          Specify the currency, if known.
 void setFractionDigits(int fractionDigits)
          Specify the desired number of fraction digits.
 void setRoundingMode(java.math.RoundingMode roundingMode)
          Specify the rounding mode to use for decimal parsing.
 
Methods inherited from class org.springframework.format.number.AbstractNumberFormatter
print, setLenient
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

roundingModeOnDecimalFormat

private static final boolean roundingModeOnDecimalFormat

fractionDigits

private int fractionDigits

roundingMode

private java.math.RoundingMode roundingMode

currency

private java.util.Currency currency
Constructor Detail

CurrencyFormatter

public CurrencyFormatter()
Method Detail

setFractionDigits

public void setFractionDigits(int fractionDigits)
Specify the desired number of fraction digits. Default is 2.


setRoundingMode

public void setRoundingMode(java.math.RoundingMode roundingMode)
Specify the rounding mode to use for decimal parsing. Default is RoundingMode.UNNECESSARY.


setCurrency

public void setCurrency(java.util.Currency currency)
Specify the currency, if known.


parse

public java.math.BigDecimal parse(java.lang.String text,
                                  java.util.Locale locale)
                           throws java.text.ParseException
Description copied from interface: Parser
Parse a text String to produce a T.

Specified by:
parse in interface Parser<java.lang.Number>
Overrides:
parse in class AbstractNumberFormatter
Parameters:
text - the text string
locale - the current user locale
Returns:
an instance of T
Throws:
java.text.ParseException - when a parse exception occurs in a java.text parsing library

getNumberFormat

protected java.text.NumberFormat getNumberFormat(java.util.Locale locale)
Description copied from class: AbstractNumberFormatter
Obtain a concrete NumberFormat for the specified locale.

Specified by:
getNumberFormat in class AbstractNumberFormatter
Parameters:
locale - the current locale
Returns:
the NumberFormat instance (never null)