]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.trend/example/org/simantics/trend/DecimalFormatDemo.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.trend / example / org / simantics / trend / DecimalFormatDemo.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.trend;\r
13 \r
14 import java.util.*;\r
15 import java.text.*;\r
16  \r
17 public class DecimalFormatDemo {\r
18  \r
19    static public void customFormat(String pattern, double value ) {\r
20       DecimalFormat myFormatter = new DecimalFormat(pattern);\r
21       String output = myFormatter.format(value);\r
22       System.out.println(value + "  " + pattern + "  " + output);\r
23    }\r
24  \r
25    static public void localizedFormat(String pattern, double value, \r
26                                       Locale loc ) {\r
27       NumberFormat nf = NumberFormat.getNumberInstance(loc);\r
28       DecimalFormat df = (DecimalFormat)nf;\r
29       df.applyPattern(pattern);\r
30       String output = df.format(value);\r
31       System.out.println(pattern + "  " + output + "  " + loc.toString());\r
32    }\r
33  \r
34    static public void main(String[] args) {\r
35  \r
36       customFormat("###,###.###", 123456.789);\r
37       customFormat("###.##", 123456.789);\r
38       customFormat("000000.000", 123.78);\r
39       customFormat("$###,###.###", 12345.67);\r
40       customFormat("\u00a5###,###.###", 12345.67);\r
41  \r
42       Locale currentLocale = new Locale("en", "US");\r
43  \r
44       DecimalFormatSymbols unusualSymbols = \r
45          new DecimalFormatSymbols(currentLocale);\r
46       unusualSymbols.setDecimalSeparator('|');\r
47       unusualSymbols.setGroupingSeparator('^');\r
48       String strange = "#,##0.###";\r
49       DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);\r
50       weirdFormatter.setGroupingSize(4);\r
51       String bizarre = weirdFormatter.format(12345.678);\r
52       System.out.println(bizarre);\r
53  \r
54       Locale[] locales = {\r
55          new Locale("en", "US"),\r
56          new Locale("de", "DE"),\r
57          new Locale("fr", "FR")\r
58       };\r
59  \r
60       for (int i = 0; i < locales.length; i++) {\r
61          localizedFormat("###,###.###", 123456.789, locales[i]);\r
62       }\r
63  \r
64    }\r
65 }