svn-gvsig-desktop / tags / v1_0_2_Build_915 / libraries / libIverUtiles / src / com / iver / utiles / DoubleUtilities.java @ 12217
History | View | Annotate | Download (3.17 KB)
1 |
package com.iver.utiles; |
---|---|
2 |
|
3 |
import java.text.DecimalFormat; |
4 |
import java.text.DecimalFormatSymbols; |
5 |
|
6 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
7 |
*
|
8 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
9 |
*
|
10 |
* This program is free software; you can redistribute it and/or
|
11 |
* modify it under the terms of the GNU General Public License
|
12 |
* as published by the Free Software Foundation; either version 2
|
13 |
* of the License, or (at your option) any later version.
|
14 |
*
|
15 |
* This program is distributed in the hope that it will be useful,
|
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
* GNU General Public License for more details.
|
19 |
*
|
20 |
* You should have received a copy of the GNU General Public License
|
21 |
* along with this program; if not, write to the Free Software
|
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
23 |
*
|
24 |
* For more information, contact:
|
25 |
*
|
26 |
* Generalitat Valenciana
|
27 |
* Conselleria d'Infraestructures i Transport
|
28 |
* Av. Blasco Ib??ez, 50
|
29 |
* 46010 VALENCIA
|
30 |
* SPAIN
|
31 |
*
|
32 |
* +34 963862235
|
33 |
* gvsig@gva.es
|
34 |
* www.gvsig.gva.es
|
35 |
*
|
36 |
* or
|
37 |
*
|
38 |
* IVER T.I. S.A
|
39 |
* Salamanca 50
|
40 |
* 46005 Valencia
|
41 |
* Spain
|
42 |
*
|
43 |
* +34 963163400
|
44 |
* dac@iver.es
|
45 |
*/
|
46 |
/* CVS MESSAGES:
|
47 |
*
|
48 |
* $Id: DoubleUtilities.java 12217 2007-06-19 10:05:46Z $
|
49 |
* $Log$
|
50 |
* Revision 1.1.4.1 2006-11-15 00:08:27 jjdelcerro
|
51 |
* *** empty log message ***
|
52 |
*
|
53 |
* Revision 1.3 2006/10/30 11:59:47 nacho
|
54 |
* formateado de double
|
55 |
*
|
56 |
* Revision 1.2 2006/09/13 08:10:07 jorpiell
|
57 |
* Se limita el n?mero de decimales por arriba y por abajo
|
58 |
*
|
59 |
* Revision 1.1 2006/05/16 13:02:41 jorpiell
|
60 |
* 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
|
61 |
*
|
62 |
*
|
63 |
*/
|
64 |
/**
|
65 |
* This class has some methods to manage "Doubles"
|
66 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
67 |
*/
|
68 |
public class DoubleUtilities { |
69 |
/**
|
70 |
* Formats a double with an specified number of decimals. It
|
71 |
* removes the separator character of the integer part.
|
72 |
* @param value
|
73 |
* Separator char of the integer part
|
74 |
* @param decimalSeparator
|
75 |
* Separator char of the decimal part
|
76 |
* @param decimalsNumber
|
77 |
* Number of decimals
|
78 |
* @return
|
79 |
* The formatted double
|
80 |
*/
|
81 |
public static double format(double value, |
82 |
char decimalSeparator,
|
83 |
int decimalsNumber){
|
84 |
|
85 |
DecimalFormat dFormat = new DecimalFormat("#"); |
86 |
DecimalFormatSymbols dFormatSymbols = new DecimalFormatSymbols(); |
87 |
|
88 |
dFormatSymbols.setDecimalSeparator(decimalSeparator); |
89 |
dFormat.setMaximumFractionDigits(decimalsNumber); |
90 |
dFormat.setMaximumFractionDigits(decimalsNumber); |
91 |
dFormat.setDecimalFormatSymbols(dFormatSymbols); |
92 |
double d = Double.parseDouble(dFormat.format(value)); |
93 |
return Double.parseDouble(dFormat.format(value)); |
94 |
} |
95 |
|
96 |
/**
|
97 |
* Formats a double with an specified number of decimals.
|
98 |
* @param num
|
99 |
* Value to tail
|
100 |
* @param n
|
101 |
* Number of decimals
|
102 |
* @return
|
103 |
* The formatted double
|
104 |
*/
|
105 |
public static double format(double num, int n){ |
106 |
long m = (long)Math.pow(10, n); |
107 |
num *= m; |
108 |
long aux = ((long)num); |
109 |
num = (double)((double)aux / (double)m); |
110 |
return num;
|
111 |
} |
112 |
|
113 |
} |