net.sourceforge.stripes.format
Class NumberFormatter

java.lang.Object
  extended by net.sourceforge.stripes.format.NumberFormatter
All Implemented Interfaces:
Formatter<Number>

public class NumberFormatter
extends Object
implements Formatter<Number>

Formats numbers into localized Strings for display. This class relies heavily on the NumberFormat and DecimalFormat classes in the java.text package, and it is suggested that you become familiar with those classes before using custom formats.

Accepts the following named formatTypes (not case sensitive):

If a format type is not supplied the default value of "number" will be used. Format String can be either a custom pattern as used by NumberFormat, or one of the following named formats (not case sensitive):

Author:
Tim Fennell

Field Summary
protected static Set<String> namedPatterns
          Maintains a set of named formats that can be used instead of patterns.
 
Constructor Summary
NumberFormatter()
           
 
Method Summary
 String format(Number input)
          Formats the number supplied as a String.
 String getFormatPattern()
          Gets the named format string or number format pattern to use to format the number.
 String getFormatType()
          Gets the format type to be used to render numbers as Strings.
 Locale getLocale()
          Gets the locale that output String should be in.
 void init()
          Instantiates the NumberFormat based on the information provided through setter methods.
 void setFormatPattern(String formatPattern)
          Sets the named format string or number format pattern to use to format the number.
 void setFormatType(String formatType)
          Sets the format type to be used to render numbers as Strings.
 void setLocale(Locale locale)
          Sets the locale that output String should be in.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

namedPatterns

protected static final Set<String> namedPatterns
Maintains a set of named formats that can be used instead of patterns.

Constructor Detail

NumberFormatter

public NumberFormatter()
Method Detail

setFormatType

public void setFormatType(String formatType)
Sets the format type to be used to render numbers as Strings.

Specified by:
setFormatType in interface Formatter<Number>

getFormatType

public String getFormatType()
Gets the format type to be used to render numbers as Strings.


setFormatPattern

public void setFormatPattern(String formatPattern)
Sets the named format string or number format pattern to use to format the number.

Specified by:
setFormatPattern in interface Formatter<Number>

getFormatPattern

public String getFormatPattern()
Gets the named format string or number format pattern to use to format the number.


setLocale

public void setLocale(Locale locale)
Sets the locale that output String should be in.

Specified by:
setLocale in interface Formatter<Number>

getLocale

public Locale getLocale()
Gets the locale that output String should be in.


init

public void init()
Instantiates the NumberFormat based on the information provided through setter methods.

Specified by:
init in interface Formatter<Number>

format

public String format(Number input)
Formats the number supplied as a String.

Specified by:
format in interface Formatter<Number>
Parameters:
input - an object of a type that the formatter knows how to format
Returns:
a non-null, String version of the input, formatted for the chosen locale


? Copyright 2005-2006, Stripes Development Team.