]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/example/org/simantics/trend/DecimalFormatDemo.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.trend / example / org / simantics / trend / DecimalFormatDemo.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.trend;
13
14 import java.util.*;
15 import java.text.*;
16  
17 public class DecimalFormatDemo {
18  
19    static public void customFormat(String pattern, double value ) {
20       DecimalFormat myFormatter = new DecimalFormat(pattern);
21       String output = myFormatter.format(value);
22       System.out.println(value + "  " + pattern + "  " + output);
23    }
24  
25    static public void localizedFormat(String pattern, double value, 
26                                       Locale loc ) {
27       NumberFormat nf = NumberFormat.getNumberInstance(loc);
28       DecimalFormat df = (DecimalFormat)nf;
29       df.applyPattern(pattern);
30       String output = df.format(value);
31       System.out.println(pattern + "  " + output + "  " + loc.toString());
32    }
33  
34    static public void main(String[] args) {
35  
36       customFormat("###,###.###", 123456.789);
37       customFormat("###.##", 123456.789);
38       customFormat("000000.000", 123.78);
39       customFormat("$###,###.###", 12345.67);
40       customFormat("\u00a5###,###.###", 12345.67);
41  
42       Locale currentLocale = new Locale("en", "US");
43  
44       DecimalFormatSymbols unusualSymbols = 
45          new DecimalFormatSymbols(currentLocale);
46       unusualSymbols.setDecimalSeparator('|');
47       unusualSymbols.setGroupingSeparator('^');
48       String strange = "#,##0.###";
49       DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
50       weirdFormatter.setGroupingSize(4);
51       String bizarre = weirdFormatter.format(12345.678);
52       System.out.println(bizarre);
53  
54       Locale[] locales = {
55          new Locale("en", "US"),
56          new Locale("de", "DE"),
57          new Locale("fr", "FR")
58       };
59  
60       for (int i = 0; i < locales.length; i++) {
61          localizedFormat("###,###.###", 123456.789, locales[i]);
62       }
63  
64    }
65 }