KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > number > Numberer_de


1 package net.sf.saxon.number;
2
3 /**
4   * Class Numberer_de is designed simply to demonstrate how to write a number formatter
5   * for a different language. This one will be activated for language="de", format="eins",
6   * letter-value="traditional"
7   * @author Michael H. Kay
8   */

9
10 public class Numberer_de extends Numberer_en {
11
12     /**
13      * Construct the ordinal suffix for a number, for example "st", "nd", "rd"
14      * @param ordinalParam the value of the ordinal attribute (used in non-English
15      * language implementations)
16      * @param number the number being formatted
17      * @return the ordinal suffix to be appended to the formatted number
18      */

19
20     protected String JavaDoc ordinalSuffix(String JavaDoc ordinalParam, long number) {
21         return ".";
22     }
23
24     /**
25     * Show the number as words in title case. (We choose title case because
26      * the result can then be converted algorithmically to lower case or upper case).
27     */

28
29     public String JavaDoc toWords(long number) {
30         if (number >= 1000000000) {
31             long rem = number % 1000000000;
32             long n = number / 100000000;
33             String JavaDoc s = (n==1 ? "Eine" : toWords(n));
34             return s + " Milliarde" +
35                     (rem==0 ? "" : " " + toWords(rem));
36         } else if (number >= 1000000) {
37             long rem = number % 1000000;
38             long n = number / 1000000;
39             String JavaDoc s = (n==1 ? "Eine" : toWords(n));
40             return s + " Million" +
41                     (rem==0 ? "" : " " + toWords(rem));
42         } else if (number >= 1000) {
43             long rem = number % 1000;
44             long n = number / 1000;
45             String JavaDoc s = (n==1 ? "Ein" : toWords(n));
46             return s + "tausend" + (rem==0 ? "" : " " + toWords(rem));
47         } else if (number >= 100) {
48             long rem = number % 100;
49             long n = number / 100;
50             String JavaDoc s = (n==1 ? "Ein" : toWords(n));
51             return s + "hundert" +
52                 (rem==0 ? "" : (rem>20 ? "" : "und") + toWords(rem, LOWER_CASE));
53         } else {
54             if (number < 20) return germanUnits[(int)number];
55             int rem = (int)(number % 10);
56             int tens = (int)number / 10;
57             return (germanUnits[rem]) +
58                     (tens==0 ? "" : (rem==0 ? "" : "und") + germanTens[tens]);
59
60         }
61     }
62
63     private static String JavaDoc[] germanUnits = {
64         "", "Eins", "Zwei", "Drei", "Vier", "Fünf", "Sechs", "Sieben", "Acht", "Neun",
65         "Zehn", "Elf", "Zwölf", "Dreizehn", "Vierzehn", "Fünfzehn", "Sechszehn",
66         "Siebzehn", "Achtzehn", "Neunzehn"};
67
68     private static String JavaDoc[] germanTens = {
69         "", "Zehn", "Zwanzig", "Dreißig", "Vierzig", "Fünfzig",
70         "Sechzig", "Siebzig", "Achtzig", "Neunzig"};
71
72     /**
73     * Show an ordinal number as German words (for example, Einundzwanzigste)
74     */

75
76     public String JavaDoc toOrdinalWords(String JavaDoc ordinalParam, long number, int wordCase) {
77         String JavaDoc suffix = "e";
78         if (ordinalParam.equalsIgnoreCase("-er")) {
79             suffix = "er";
80         } else if (ordinalParam.equalsIgnoreCase("-es")) {
81             suffix = "es";
82         } else if (ordinalParam.equalsIgnoreCase("-en")) {
83             suffix = "en";
84         }
85         long mod100 = number % 100;
86         if (number < 20) {
87             String JavaDoc ord = germanOrdinalUnits[(int)number] + suffix;
88             if (wordCase==UPPER_CASE) {
89                 return ord.toUpperCase();
90             } else if (wordCase==LOWER_CASE) {
91                 return ord.toLowerCase();
92             } else {
93                 return ord;
94             }
95         } else if (mod100 < 20 && mod100 > 0) {
96             return toWords(number - (mod100), wordCase) +
97                     toOrdinalWords(ordinalParam, mod100,
98                                         (wordCase==TITLE_CASE ? LOWER_CASE : wordCase));
99         } else {
100             String JavaDoc ending = "st" + suffix;
101             if (wordCase==UPPER_CASE) {
102                 ending = ending.toUpperCase();
103             }
104             return toWords(number, wordCase) +
105                     (wordCase==UPPER_CASE ? ending.toUpperCase() : ending);
106         }
107     }
108
109     private static String JavaDoc[] germanOrdinalUnits = {
110         "", "Erst", "Zweit", "Dritt", "Viert", "Fünft", "Sechst", "Siebt", "Acht", "Neunt",
111         "Zehnt", "Elft", "Zwölft", "Dreizehnt", "Vierzehnt", "Fünfzehnt", "Sechszehnt",
112         "Siebzehnt", "Achtzehnt", "Neunzehnt"};
113 //
114
// private static String[] germanOrdinalTens = {
115
// "", "Tenth", "Twentieth", "Thirtieth", "Fortieth", "Fiftieth",
116
// "Sixtieth", "Seventieth", "Eightieth", "Ninetieth"};
117

118     /**
119      * Get a month name or abbreviation
120      * @param month The month number (1=January, 12=December)
121      * @param minWidth The minimum number of characters
122      * @param maxWidth The maximum number of characters
123      */

124
125     public String JavaDoc monthName(int month, int minWidth, int maxWidth) {
126         String JavaDoc name = germanMonths[month-1];
127         if (maxWidth < 3) {
128             maxWidth = 3;
129         }
130         if (name.length() > maxWidth) {
131             name = name.substring(0, maxWidth);
132         }
133         while (name.length() < minWidth) {
134             name = name + " ";
135         }
136         return name;
137     }
138
139     private static String JavaDoc[] germanMonths = {
140         "Januar", "Februar", "März", "April", "Mai", "Juni",
141         "Juli", "August", "September", "Oktober", "November", "Dezember"
142     };
143
144     /**
145      * Get a day name or abbreviation
146      * @param day The month number (1=Sunday, 7=Saturday)
147      * @param minWidth The minimum number of characters
148      * @param maxWidth The maximum number of characters
149      */

150
151     public String JavaDoc dayName(int day, int minWidth, int maxWidth) {
152         String JavaDoc name = germanDays[day-1];
153         if (maxWidth < 10) {
154             name = name.substring(0, 2);
155         }
156         while (name.length() < minWidth) {
157             name = name + " ";
158         }
159         return name;
160     }
161
162     private static String JavaDoc[] germanDays = {
163         "Sunday", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag",
164     };
165
166     /**
167      * Get an ordinal suffix for a particular component of a date/time.
168      *
169      * @param component the component specifier from a format-dateTime picture, for
170      * example "M" for the month or "D" for the day.
171      * @return a string that is acceptable in the ordinal attribute of xsl:number
172      * to achieve the required ordinal representation. For example, "-e" for the day component
173      * in German, to have the day represented as "dritte August".
174      */

175
176     public String JavaDoc getOrdinalSuffixForDateTime(String JavaDoc component) {
177         return "-e";
178     }
179
180 }
181
182 //
183
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
184
// you may not use this file except in compliance with the License. You may obtain a copy of the
185
// License at http://www.mozilla.org/MPL/
186
//
187
// Software distributed under the License is distributed on an "AS IS" basis,
188
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
189
// See the License for the specific language governing rights and limitations under the License.
190
//
191
// The Original Code is: all this file.
192
//
193
// The Initial Developer of the Original Code is Michael H. Kay.
194
//
195
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
196
//
197
// Contributor(s): none.
198
//
199
Popular Tags