Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.library / org.gvsig.utils / src / main / java / org / gvsig / utils / DoubleUtilities.java @ 40561

History | View | Annotate | Download (4.08 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
package org.gvsig.utils;
25

    
26
import java.text.DecimalFormat;
27
import java.text.DecimalFormatSymbols;
28

    
29
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
30
 *
31
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
32
 *
33
 * This program is free software; you can redistribute it and/or
34
 * modify it under the terms of the GNU General Public License
35
 * as published by the Free Software Foundation; either version 2
36
 * of the License, or (at your option) any later version.
37
 *
38
 * This program is distributed in the hope that it will be useful,
39
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
41
 * GNU General Public License for more details.
42
 *
43
 * You should have received a copy of the GNU General Public License
44
 * along with this program; if not, write to the Free Software
45
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
46
 *
47
 * For more information, contact:
48
 *
49
 *  Generalitat Valenciana
50
 *   Conselleria d'Infraestructures i Transport
51
 *   Av. Blasco Ib??ez, 50
52
 *   46010 VALENCIA
53
 *   SPAIN
54
 *
55
 *      +34 963862235
56
 *   gvsig@gva.es
57
 *      www.gvsig.gva.es
58
 *
59
 *    or
60
 *
61
 *   IVER T.I. S.A
62
 *   Salamanca 50
63
 *   46005 Valencia
64
 *   Spain
65
 *
66
 *   +34 963163400
67
 *   dac@iver.es
68
 */
69
/* CVS MESSAGES:
70
 *
71
 * $Id: DoubleUtilities.java 29631 2009-06-29 16:56:19Z jpiera $
72
 * $Log$
73
 * Revision 1.3  2006-10-30 11:59:47  nacho
74
 * formateado de double
75
 *
76
 * Revision 1.2  2006/09/13 08:10:07  jorpiell
77
 * Se limita el n?mero de decimales por arriba y por abajo
78
 *
79
 * Revision 1.1  2006/05/16 13:02:41  jorpiell
80
 * Se ha a?adido la clase DoubleUtiles, donde ha un metodo para limitar el tama?o de los decimales de un double y para quitar los "puntos" de la parte entera
81
 *
82
 *
83
 */
84
/**
85
 * This class has some methods to manage "Doubles"
86
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
87
 */
88
public class DoubleUtilities {
89
        /**
90
         * Formats a double with an specified number of decimals. It 
91
         * removes the separator character of the integer part. 
92
         * @param value
93
         * Separator char of the integer part
94
         * @param decimalSeparator
95
         * Separator char of the decimal part
96
         * @param decimalsNumber
97
         * Number of decimals
98
         * @return
99
         * The formatted double
100
         */
101
        public static double format(double value,
102
                        char decimalSeparator,
103
                        int decimalsNumber){
104
                        
105
                DecimalFormat dFormat = new DecimalFormat("#");
106
                DecimalFormatSymbols dFormatSymbols = new DecimalFormatSymbols();
107
                
108
                dFormatSymbols.setDecimalSeparator(decimalSeparator);
109
                dFormat.setMaximumFractionDigits(decimalsNumber);
110
                dFormat.setMaximumFractionDigits(decimalsNumber);
111
                dFormat.setDecimalFormatSymbols(dFormatSymbols);
112
                double d = Double.parseDouble(dFormat.format(value));
113
                return Double.parseDouble(dFormat.format(value));                
114
        }
115

    
116
        /**
117
         * Formats a double with an specified number of decimals. 
118
         * @param num
119
         * Value to tail
120
         * @param n
121
         * Number of decimals
122
         * @return
123
         * The formatted double
124
         */
125
    public static double format(double num, int n){
126
            long m = (long)Math.pow(10, n);
127
        num *= m;
128
        long aux = ((long)num);
129
        num = (double)((double)aux / (double)m);
130
        return num;
131
    }
132

    
133
}