KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > infra > DigitFormat


1 package org.javabb.infra;
2
3 import java.text.DecimalFormat JavaDoc;
4 import java.text.DecimalFormatSymbols JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8
9 /*
10  * Copyright 2004 JavaFree.org
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24
25 /**
26  * $Id: DigitFormat.java,v 1.1.10.2 2006/04/17 17:47:15 daltoncamargo Exp $
27  *
28  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org</a>
29  */

30 public class DigitFormat {
31
32     protected static Log log = LogFactory.getLog(DigitFormat.class);
33     
34     private static DecimalFormat JavaDoc currencyFormat() {
35         DecimalFormat JavaDoc dec = new DecimalFormat JavaDoc(
36                 "#,###");
37         DecimalFormatSymbols JavaDoc decDef = new DecimalFormatSymbols JavaDoc();
38         decDef.setZeroDigit('0');
39         decDef.setDecimalSeparator(',');
40         decDef.setMonetaryDecimalSeparator(',');
41         decDef.setDigit('#');
42         decDef.setGroupingSeparator('.');
43         dec.setDecimalFormatSymbols(decDef);
44         return dec;
45     }
46
47     /**
48      * Faz o parser de um valor STRING
49      * @param valor
50      * @return
51      */

52     public static String JavaDoc parserValue(String JavaDoc paramValue) {
53         String JavaDoc valor = currencyFormat().format(new Double JavaDoc(paramValue));
54         String JavaDoc tmpVlr = valor;
55         if(valor.length() > 4){
56             tmpVlr = valor.substring(valor.length() -4, valor.length());
57             if(tmpVlr.equals("0000")){
58                 tmpVlr = valor.substring(0, valor.length()-4);
59             } else {
60                 tmpVlr = valor;
61             }
62         }
63         return tmpVlr;
64     }
65     
66     public static void main(String JavaDoc[] args){
67         System.out.println(DigitFormat.parserValue("1.5435555E7"));
68     }
69
70 }
Popular Tags