]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils/src/org/simantics/utils/format/ValueFormat.java
Make ValueFormat.toFormat safe to call with any noOfDecimals value
[simantics/platform.git] / bundles / org.simantics.utils / src / org / simantics / utils / format / ValueFormat.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.format;
13
14 import java.text.DecimalFormat;
15 import java.text.DecimalFormatSymbols;
16 import java.text.Format;
17 import java.text.NumberFormat;
18 import java.util.Locale;
19
20
21 /**
22  * 
23  * @author toni.kalajainen
24  */
25 public enum ValueFormat {
26         
27         Currency ( new DecimalFormat("#,##0.#######;-#,##0.#####", Impl.SYMBOLS) ),
28         Scientific( new DecimalFormat("0.###E0", Impl.SYMBOLS) ),
29         Engineering( new DecimalFormat("##0.###E0", Impl.SYMBOLS) ),
30         Default ( new SwitchFormat(1e-5, 1e5, Scientific.format, Currency.format, Scientific.format) );
31
32         public final NumberFormat format;
33         ValueFormat(NumberFormat format) { this.format = format; };
34         
35         public Format toFormat( int noOfDecimals ) 
36         {
37                 switch ( this ) {
38                         case Currency: return noOfDecimals>decimalFormats.length||noOfDecimals<0 ? decimalFormats[decimalFormats.length-1] : decimalFormats[Math.min(noOfDecimals, 16)]; 
39                         case Scientific: return format;
40                         case Engineering: return format;
41                         case Default: return noOfDecimals>defaultFormats.length||noOfDecimals<0 ? defaultFormats[defaultFormats.length-1] : defaultFormats[Math.min(noOfDecimals, 16)];
42                         default: return format;
43                 }
44         }
45         
46         public static void main(String[] args) {
47                 double value = 1234567.8901234567890123456;
48                 for (int i=0; i<10; i++) {
49                         System.out.println("value="+value+", decimalformat["+i+"]="+decimalFormats[i].format(value));
50                 }
51                 value = 567.8901234567890123456;
52                 for (int i=0; i<10; i++) {
53                         System.out.println("value="+value+", decimalformat["+i+"]="+decimalFormats[i].format(value));
54                 }
55         }
56         
57         private static DecimalFormat decimalFormats[] = new DecimalFormat[17];
58         private static Format defaultFormats[] = new Format[17];
59         
60         static {
61                 String decimals = "";
62                 for ( int i=0; i<decimalFormats.length; i++ ) {
63                         decimalFormats[i] = new DecimalFormat("#,###"+decimals+";-#,###"+decimals, Impl.SYMBOLS);
64                         defaultFormats[i] = new SwitchFormat(1e-5, 1e5, Scientific.format, decimalFormats[i], Scientific.format);
65                         if ( i==0 ) decimals += ".";
66                         decimals += "#";
67                 }
68         }
69
70         private static class Impl {
71                 private static final DecimalFormatSymbols SYMBOLS;
72                 static {
73                         SYMBOLS = DecimalFormatSymbols.getInstance(Locale.US);
74                         SYMBOLS.setGroupingSeparator(' ');
75                 }
76         }
77
78 }