Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members  

TFixedPointDecimalTranslator.java

00001 /*
00002 *  Name:      TFixedPointDecimalTranslator.java
00003 *  Author:    Rafael Jesus Alcantara Perez
00004 *  Summary:   Class for translating numbers to its fixed point decimal
00005 *             string representation and vice-versa using locale.
00006 *  Date:      $Date: 2003/10/06 12:45:11 $
00007 *  Revision:  $Revision: 1.5 $
00008 *
00009 *  Copyright (C) 2001-2003  Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com>
00010 *
00011 *  This program is free software; you can redistribute it and/or modify
00012 *  it under the terms of the GNU General Public License as published by
00013 *  the Free Software Foundation; either version 2 of the License, or
00014 *  (at your option) any later version.
00015 *
00016 *  This program is distributed in the hope that it will be useful,
00017 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 *  GNU General Public License for more details.
00020 *
00021 *  You should have received a copy of the GNU General Public License
00022 *  along with this program; if not, write to the Free Software
00023 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024 *  MA 02111-1307, USA.
00025 */
00026 
00028 package org.mpcl.text;
00029 
00030 import java.text.DecimalFormat;
00031 import java.text.NumberFormat;
00032 import java.text.ParseException;
00033 import java.util.Locale;
00034 
00035 
00040 public class TFixedPointDecimalTranslator extends TAbstractNumberTranslator
00041 {
00042   
00044   DecimalFormat   tDecimalFormat;
00045 
00046 
00047   //
00048   //  C O N S T R U C T O R S
00049   //
00050 
00051   private void initialize()
00052   {
00053     tDecimalFormat.setDecimalSeparatorAlwaysShown (true);
00054     setFractionDigits (2);
00055   }
00056 
00057   private void setFractionDigits (int iFRACTION_DIGITS)
00058   {
00059     tDecimalFormat.setMaximumFractionDigits (iFRACTION_DIGITS);
00060     tDecimalFormat.setMinimumFractionDigits (iFRACTION_DIGITS);
00061   }
00062 
00063 
00064   //
00065   //  C O N S T R U C T O R S
00066   //
00067 
00069   public TFixedPointDecimalTranslator()
00070   {
00071     tDecimalFormat = (DecimalFormat) NumberFormat.getInstance();
00072     initialize();
00073   }
00074 
00079   public TFixedPointDecimalTranslator (Locale tLOCALE)
00080   {
00081     tDecimalFormat = (DecimalFormat) NumberFormat.getInstance (tLOCALE);
00082     initialize();
00083   }
00084 
00085 
00086   //
00087   //  S E L E C T O R S
00088   //
00089 
00098   public double toDouble (String yNUMBER) throws ParseException
00099   {
00100     return tDecimalFormat.parse (yNUMBER).doubleValue();
00101   }
00102 
00111   public float toFloat (String yNUMBER) throws ParseException
00112   {
00113     return tDecimalFormat.parse (yNUMBER).floatValue();
00114   }
00115 
00124   public int toInt (String yNUMBER) throws ParseException
00125   {
00126     return tDecimalFormat.parse (yNUMBER).intValue();
00127   }
00128 
00137   public String toString (double dNUMBER)
00138   {
00139     return tDecimalFormat.format (dNUMBER);
00140   }
00141 
00150   public String toString (Number tNUMBER)
00151   {
00152     return tDecimalFormat.format (tNUMBER);
00153   }
00154 
00155 }  // class TFixedPointDecimalTranslator

Generated on Mon Oct 13 02:35:24 2003 for MPCL by doxygen1.2.18