2 // System.Globalization.NumberFormatInfo.cs
5 // Derek Holden (dholden@draper.com)
6 // Bob Smith (bob@thestuff.net)
7 // Mohammad DAMT (mdamt@cdl2000.com)
10 // (C) Bob Smith http://www.thestuff.net
11 // (c) 2003, PT Cakram Datalingga Duaribu http://www.cdl2000.com
15 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
17 // Permission is hereby granted, free of charge, to any person obtaining
18 // a copy of this software and associated documentation files (the
19 // "Software"), to deal in the Software without restriction, including
20 // without limitation the rights to use, copy, modify, merge, publish,
21 // distribute, sublicense, and/or sell copies of the Software, and to
22 // permit persons to whom the Software is furnished to do so, subject to
23 // the following conditions:
25 // The above copyright notice and this permission notice shall be
26 // included in all copies or substantial portions of the Software.
28 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 // NumberFormatInfo. One can only assume it is the class gotten
39 // back from a GetFormat() method from an IFormatProvider /
40 // IFormattable implementer. There are some discrepencies with the
41 // ECMA spec and the SDK docs, surprisingly. See my conversation
42 // with myself on it at:
43 // http://lists.ximian.com/archives/public/mono-list/2001-July/000794.html
45 // Other than that this is totally ECMA compliant.
48 namespace System.Globalization {
51 public sealed class NumberFormatInfo : ICloneable, IFormatProvider {
52 private bool isReadOnly;
53 // used for temporary storage. Used in InitPatterns ()
54 string decimalFormats;
55 string currencyFormats;
56 string percentFormats;
57 string digitPattern = "#";
58 string zeroPattern = "0";
60 // Currency Related Format Info
61 private int currencyDecimalDigits;
62 private string currencyDecimalSeparator;
63 private string currencyGroupSeparator;
64 private int[] currencyGroupSizes;
65 private int currencyNegativePattern;
66 private int currencyPositivePattern;
67 private string currencySymbol;
69 private string nanSymbol;
70 private string negativeInfinitySymbol;
71 private string negativeSign;
73 // Number Related Format Info
74 private int numberDecimalDigits;
75 private string numberDecimalSeparator;
76 private string numberGroupSeparator;
77 private int[] numberGroupSizes;
78 private int numberNegativePattern;
80 // Percent Related Format Info
81 private int percentDecimalDigits;
82 private string percentDecimalSeparator;
83 private string percentGroupSeparator;
84 private int[] percentGroupSizes;
85 private int percentNegativePattern;
86 private int percentPositivePattern;
87 private string percentSymbol;
89 private string perMilleSymbol;
90 private string positiveInfinitySymbol;
91 private string positiveSign;
93 string ansiCurrencySymbol; // TODO, MS.NET serializes this.
94 int m_dataItem; // Unused, but MS.NET serializes this.
95 bool m_useUserOverride; // Unused, but MS.NET serializes this.
96 bool validForParseAsNumber; // Unused, but MS.NET serializes this.
97 bool validForParseAsCurrency; // Unused, but MS.NET serializes this.
99 internal NumberFormatInfo (int lcid)
101 //FIXME: should add more LCID
102 // CultureInfo uses this one also.
108 // The Invariant Culture Info ID.
112 // Currency Related Format Info
113 currencyDecimalDigits = 2;
114 currencyDecimalSeparator = ".";
115 currencyGroupSeparator = ",";
116 currencyGroupSizes = new int[1] { 3 };
117 currencyNegativePattern = 0;
118 currencyPositivePattern = 0;
119 currencySymbol = "$";
122 negativeInfinitySymbol = "-Infinity";
125 // Number Related Format Info
126 numberDecimalDigits = 2;
127 numberDecimalSeparator = ".";
128 numberGroupSeparator = ",";
129 numberGroupSizes = new int[1] { 3 };
130 numberNegativePattern = 1;
132 // Percent Related Format Info
133 percentDecimalDigits = 2;
134 percentDecimalSeparator = ".";
135 percentGroupSeparator = ",";
136 percentGroupSizes = new int[1] { 3 };
137 percentNegativePattern = 0;
138 percentPositivePattern = 0;
141 perMilleSymbol = "\u2030";
142 positiveInfinitySymbol = "Infinity";
148 public NumberFormatInfo () : this (0x007f)
152 // this is called by mono/mono/metadata/locales.c
155 string [] partOne, partTwo;
156 string [] posNeg = decimalFormats.Split (new char [1] {';'}, 2);
158 if (posNeg.Length == 2) {
160 partOne = posNeg [0].Split (new char [1] {'.'}, 2);
162 if (partOne.Length == 2) {
163 // assumed same for both positive and negative
164 // decimal digit side
165 numberDecimalDigits = 0;
166 for (int i = 0; i < partOne [1].Length; i ++) {
167 if (partOne [1][i] == digitPattern [0]) {
168 numberDecimalDigits ++;
173 // decimal grouping side
174 partTwo = partOne [0].Split (',');
175 if (partTwo.Length > 1) {
176 numberGroupSizes = new int [partTwo.Length - 1];
177 for (int i = 0; i < numberGroupSizes.Length; i ++) {
178 string pat = partTwo [i + 1];
179 numberGroupSizes [i] = pat.Length;
182 numberGroupSizes = new int [1] { 0 };
185 if (posNeg [1].StartsWith ("(") && posNeg [1].EndsWith (")")) {
186 numberNegativePattern = 0;
187 } else if (posNeg [1].StartsWith ("- ")) {
188 numberNegativePattern = 2;
189 } else if (posNeg [1].StartsWith ("-")) {
190 numberNegativePattern = 1;
191 } else if (posNeg [1].EndsWith (" -")) {
192 numberNegativePattern = 4;
193 } else if (posNeg [1].EndsWith ("-")) {
194 numberNegativePattern = 3;
196 numberNegativePattern = 1;
201 posNeg = currencyFormats.Split (new char [1] {';'}, 2);
202 if (posNeg.Length == 2) {
203 partOne = posNeg [0].Split (new char [1] {'.'}, 2);
205 if (partOne.Length == 2) {
206 // assumed same for both positive and negative
207 // decimal digit side
208 currencyDecimalDigits = 0;
209 for (int i = 0; i < partOne [1].Length; i ++) {
210 if (partOne [1][i] == zeroPattern [0])
211 currencyDecimalDigits ++;
216 // decimal grouping side
217 partTwo = partOne [0].Split (',');
218 if (partTwo.Length > 1) {
219 currencyGroupSizes = new int [partTwo.Length - 1];
220 for (int i = 0; i < currencyGroupSizes.Length; i ++) {
221 string pat = partTwo [i + 1];
222 currencyGroupSizes [i] = pat.Length;
225 currencyGroupSizes = new int [1] { 0 };
228 if (posNeg [1].StartsWith ("(\u00a4 ") && posNeg [1].EndsWith (")")) {
229 currencyNegativePattern = 14;
230 } else if (posNeg [1].StartsWith ("(\u00a4") && posNeg [1].EndsWith (")")) {
231 currencyNegativePattern = 0;
232 } else if (posNeg [1].StartsWith ("\u00a4 ") && posNeg [1].EndsWith ("-")) {
233 currencyNegativePattern = 11;
234 } else if (posNeg [1].StartsWith ("\u00a4") && posNeg [1].EndsWith ("-")) {
235 currencyNegativePattern = 3;
236 } else if (posNeg [1].StartsWith ("(") && posNeg [1].EndsWith (" \u00a4")) {
237 currencyNegativePattern = 15;
238 } else if (posNeg [1].StartsWith ("(") && posNeg [1].EndsWith ("\u00a4")) {
239 currencyNegativePattern = 4;
240 } else if (posNeg [1].StartsWith ("-") && posNeg [1].EndsWith (" \u00a4")) {
241 currencyNegativePattern = 8;
242 } else if (posNeg [1].StartsWith ("-") && posNeg [1].EndsWith ("\u00a4")) {
243 currencyNegativePattern = 5;
244 } else if (posNeg [1].StartsWith ("-\u00a4 ")) {
245 currencyNegativePattern = 9;
246 } else if (posNeg [1].StartsWith ("-\u00a4")) {
247 currencyNegativePattern = 1;
248 } else if (posNeg [1].StartsWith ("\u00a4 -")) {
249 currencyNegativePattern = 12;
250 } else if (posNeg [1].StartsWith ("\u00a4-")) {
251 currencyNegativePattern = 2;
252 } else if (posNeg [1].EndsWith (" \u00a4-")) {
253 currencyNegativePattern = 10;
254 } else if (posNeg [1].EndsWith ("\u00a4-")) {
255 currencyNegativePattern = 7;
256 } else if (posNeg [1].EndsWith ("- \u00a4")) {
257 currencyNegativePattern = 13;
258 } else if (posNeg [1].EndsWith ("-\u00a4")) {
259 currencyNegativePattern = 6;
261 currencyNegativePattern = 0;
264 if (posNeg [0].StartsWith ("\u00a4 ")) {
265 currencyPositivePattern = 2;
266 } else if (posNeg [0].StartsWith ("\u00a4")) {
267 currencyPositivePattern = 0;
268 } else if (posNeg [0].EndsWith (" \u00a4")) {
269 currencyPositivePattern = 3;
270 } else if (posNeg [0].EndsWith ("\u00a4")) {
271 currencyPositivePattern = 1;
273 currencyPositivePattern = 0;
278 // we don't have percentNegativePattern in CLDR so
279 // the percentNegativePattern are just guesses
280 if (percentFormats.StartsWith ("%")) {
281 percentPositivePattern = 2;
282 percentNegativePattern = 2;
283 } else if (percentFormats.EndsWith (" %")) {
284 percentPositivePattern = 0;
285 percentNegativePattern = 0;
286 } else if (percentFormats.EndsWith ("%")) {
287 percentPositivePattern = 1;
288 percentNegativePattern = 1;
290 percentPositivePattern = 0;
291 percentNegativePattern = 0;
294 partOne = percentFormats.Split (new char [1] {'.'}, 2);
296 if (partOne.Length == 2) {
297 // assumed same for both positive and negative
298 // decimal digit side
299 percentDecimalDigits = 0;
300 for (int i = 0; i < partOne [1].Length; i ++) {
301 if (partOne [1][i] == digitPattern [0])
302 percentDecimalDigits ++;
307 // percent grouping side
308 partTwo = partOne [0].Split (',');
309 if (partTwo.Length > 1) {
310 percentGroupSizes = new int [partTwo.Length - 1];
311 for (int i = 0; i < percentGroupSizes.Length; i ++) {
312 string pat = partTwo [i + 1];
313 percentGroupSizes [i] = pat.Length;
316 percentGroupSizes = new int [1] { 0 };
322 // =========== Currency Format Properties =========== //
324 public int CurrencyDecimalDigits {
326 return currencyDecimalDigits;
330 if (value < 0 || value > 99)
331 throw new ArgumentOutOfRangeException
332 ("The value specified for the property is less than 0 or greater than 99");
335 throw new InvalidOperationException
336 ("The current instance is read-only and a set operation was attempted");
338 currencyDecimalDigits = value;
342 public string CurrencyDecimalSeparator {
344 return currencyDecimalSeparator;
349 throw new ArgumentNullException
350 ("The value specified for the property is a null reference");
353 throw new InvalidOperationException
354 ("The current instance is read-only and a set operation was attempted");
356 currencyDecimalSeparator = value;
361 public string CurrencyGroupSeparator {
363 return currencyGroupSeparator;
368 throw new ArgumentNullException
369 ("The value specified for the property is a null reference");
372 throw new InvalidOperationException
373 ("The current instance is read-only and a set operation was attempted");
375 currencyGroupSeparator = value;
379 public int[] CurrencyGroupSizes {
381 return (int []) currencyGroupSizes.Clone ();
386 throw new ArgumentNullException
387 ("The value specified for the property is a null reference");
390 throw new InvalidOperationException
391 ("The current instance is read-only and a set operation was attempted");
393 if (value.Length == 0) {
394 currencyGroupSizes = new int [0];
398 // All elements except last need to be in range 1 - 9, last can be 0.
399 int last = value.Length - 1;
401 for (int i = 0; i < last; i++)
402 if (value[i] < 1 || value[i] > 9)
403 throw new ArgumentOutOfRangeException
404 ("One of the elements in the array specified is not between 1 and 9");
406 if (value[last] < 0 || value[last] > 9)
407 throw new ArgumentOutOfRangeException
408 ("Last element in the array specified is not between 0 and 9");
410 currencyGroupSizes = (int[]) value.Clone();
414 public int CurrencyNegativePattern {
416 // See ECMA NumberFormatInfo page 8
417 return currencyNegativePattern;
421 if (value < 0 || value > 15)
422 throw new ArgumentOutOfRangeException
423 ("The value specified for the property is less than 0 or greater than 15");
426 throw new InvalidOperationException
427 ("The current instance is read-only and a set operation was attempted");
429 currencyNegativePattern = value;
433 public int CurrencyPositivePattern {
435 // See ECMA NumberFormatInfo page 11
436 return currencyPositivePattern;
440 if (value < 0 || value > 3)
441 throw new ArgumentOutOfRangeException
442 ("The value specified for the property is less than 0 or greater than 3");
445 throw new InvalidOperationException
446 ("The current instance is read-only and a set operation was attempted");
448 currencyPositivePattern = value;
452 public string CurrencySymbol {
454 return currencySymbol;
459 throw new ArgumentNullException
460 ("The value specified for the property is a null reference");
463 throw new InvalidOperationException
464 ("The current instance is read-only and a set operation was attempted");
466 currencySymbol = value;
470 // =========== Static Read-Only Properties =========== //
472 public static NumberFormatInfo CurrentInfo {
474 NumberFormatInfo nfi = (NumberFormatInfo) System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;
475 nfi.isReadOnly = true;
480 public static NumberFormatInfo InvariantInfo {
482 // This uses invariant info, which is same as in the constructor
483 NumberFormatInfo nfi = new NumberFormatInfo ();
484 nfi.NumberNegativePattern = 1;
485 nfi.isReadOnly = true;
490 public bool IsReadOnly {
498 public string NaNSymbol {
505 throw new ArgumentNullException
506 ("The value specified for the property is a null reference");
509 throw new InvalidOperationException
510 ("The current instance is read-only and a set operation was attempted");
516 public string NegativeInfinitySymbol {
518 return negativeInfinitySymbol;
523 throw new ArgumentNullException
524 ("The value specified for the property is a null reference");
527 throw new InvalidOperationException
528 ("The current instance is read-only and a set operation was attempted");
530 negativeInfinitySymbol = value;
534 public string NegativeSign {
541 throw new ArgumentNullException
542 ("The value specified for the property is a null reference");
545 throw new InvalidOperationException
546 ("The current instance is read-only and a set operation was attempted");
548 negativeSign = value;
552 // =========== Number Format Properties =========== //
554 public int NumberDecimalDigits {
556 return numberDecimalDigits;
560 if (value < 0 || value > 99)
561 throw new ArgumentOutOfRangeException
562 ("The value specified for the property is less than 0 or greater than 99");
565 throw new InvalidOperationException
566 ("The current instance is read-only and a set operation was attempted");
568 numberDecimalDigits = value;
572 public string NumberDecimalSeparator {
574 return numberDecimalSeparator;
579 throw new ArgumentNullException
580 ("The value specified for the property is a null reference");
583 throw new InvalidOperationException
584 ("The current instance is read-only and a set operation was attempted");
586 numberDecimalSeparator = value;
591 public string NumberGroupSeparator {
593 return numberGroupSeparator;
598 throw new ArgumentNullException
599 ("The value specified for the property is a null reference");
602 throw new InvalidOperationException
603 ("The current instance is read-only and a set operation was attempted");
605 numberGroupSeparator = value;
609 public int[] NumberGroupSizes {
611 return (int []) numberGroupSizes.Clone ();
616 throw new ArgumentNullException
617 ("The value specified for the property is a null reference");
620 throw new InvalidOperationException
621 ("The current instance is read-only and a set operation was attempted");
623 if (value.Length == 0) {
624 numberGroupSizes = new int [0];
627 // All elements except last need to be in range 1 - 9, last can be 0.
628 int last = value.Length - 1;
630 for (int i = 0; i < last; i++)
631 if (value[i] < 1 || value[i] > 9)
632 throw new ArgumentOutOfRangeException
633 ("One of the elements in the array specified is not between 1 and 9");
635 if (value[last] < 0 || value[last] > 9)
636 throw new ArgumentOutOfRangeException
637 ("Last element in the array specified is not between 0 and 9");
639 numberGroupSizes = (int[]) value.Clone();
643 public int NumberNegativePattern {
645 // See ECMA NumberFormatInfo page 27
646 return numberNegativePattern;
650 if (value < 0 || value > 4)
651 throw new ArgumentOutOfRangeException
652 ("The value specified for the property is less than 0 or greater than 15");
655 throw new InvalidOperationException
656 ("The current instance is read-only and a set operation was attempted");
658 numberNegativePattern = value;
662 // =========== Percent Format Properties =========== //
664 public int PercentDecimalDigits {
666 return percentDecimalDigits;
670 if (value < 0 || value > 99)
671 throw new ArgumentOutOfRangeException
672 ("The value specified for the property is less than 0 or greater than 99");
675 throw new InvalidOperationException
676 ("The current instance is read-only and a set operation was attempted");
678 percentDecimalDigits = value;
682 public string PercentDecimalSeparator {
684 return percentDecimalSeparator;
689 throw new ArgumentNullException
690 ("The value specified for the property is a null reference");
693 throw new InvalidOperationException
694 ("The current instance is read-only and a set operation was attempted");
696 percentDecimalSeparator = value;
701 public string PercentGroupSeparator {
703 return percentGroupSeparator;
708 throw new ArgumentNullException
709 ("The value specified for the property is a null reference");
712 throw new InvalidOperationException
713 ("The current instance is read-only and a set operation was attempted");
715 percentGroupSeparator = value;
719 public int[] PercentGroupSizes {
721 return (int []) percentGroupSizes.Clone ();
726 throw new ArgumentNullException
727 ("The value specified for the property is a null reference");
730 throw new InvalidOperationException
731 ("The current instance is read-only and a set operation was attempted");
733 if (this == CultureInfo.CurrentCulture.NumberFormat)
734 throw new Exception ("HERE the value was modified");
736 if (value.Length == 0) {
737 percentGroupSizes = new int [0];
741 // All elements except last need to be in range 1 - 9, last can be 0.
742 int last = value.Length - 1;
744 for (int i = 0; i < last; i++)
745 if (value[i] < 1 || value[i] > 9)
746 throw new ArgumentOutOfRangeException
747 ("One of the elements in the array specified is not between 1 and 9");
749 if (value[last] < 0 || value[last] > 9)
750 throw new ArgumentOutOfRangeException
751 ("Last element in the array specified is not between 0 and 9");
753 percentGroupSizes = (int[]) value.Clone();
757 public int PercentNegativePattern {
759 // See ECMA NumberFormatInfo page 8
760 return percentNegativePattern;
764 if (value < 0 || value > 2)
765 throw new ArgumentOutOfRangeException
766 ("The value specified for the property is less than 0 or greater than 15");
769 throw new InvalidOperationException
770 ("The current instance is read-only and a set operation was attempted");
772 percentNegativePattern = value;
776 public int PercentPositivePattern {
778 // See ECMA NumberFormatInfo page 11
779 return percentPositivePattern;
783 if (value < 0 || value > 2)
784 throw new ArgumentOutOfRangeException
785 ("The value specified for the property is less than 0 or greater than 3");
788 throw new InvalidOperationException
789 ("The current instance is read-only and a set operation was attempted");
791 percentPositivePattern = value;
795 public string PercentSymbol {
797 return percentSymbol;
802 throw new ArgumentNullException
803 ("The value specified for the property is a null reference");
806 throw new InvalidOperationException
807 ("The current instance is read-only and a set operation was attempted");
809 percentSymbol = value;
813 public string PerMilleSymbol {
815 return perMilleSymbol;
820 throw new ArgumentNullException
821 ("The value specified for the property is a null reference");
824 throw new InvalidOperationException
825 ("The current instance is read-only and a set operation was attempted");
827 perMilleSymbol = value;
831 public string PositiveInfinitySymbol {
833 return positiveInfinitySymbol;
838 throw new ArgumentNullException
839 ("The value specified for the property is a null reference");
842 throw new InvalidOperationException
843 ("The current instance is read-only and a set operation was attempted");
845 positiveInfinitySymbol = value;
849 public string PositiveSign {
856 throw new ArgumentNullException
857 ("The value specified for the property is a null reference");
860 throw new InvalidOperationException
861 ("The current instance is read-only and a set operation was attempted");
863 positiveSign = value;
867 public object GetFormat (Type formatType)
869 // work around http://bugzilla.ximian.com/show_bug.cgi?id=55978
870 // the comparison fails because formatType likely comes from another domain
871 //return (formatType == typeof (NumberFormatInfo)) ? this : null;
875 public object Clone ()
877 NumberFormatInfo clone = (NumberFormatInfo) MemberwiseClone();
878 // clone is not read only
879 clone.isReadOnly = false;
883 public static NumberFormatInfo ReadOnly (NumberFormatInfo nfi)
885 NumberFormatInfo copy = (NumberFormatInfo)nfi.Clone();
886 copy.isReadOnly = true;
890 public static NumberFormatInfo GetInstance(IFormatProvider provider)
892 if (provider != null) {
893 NumberFormatInfo nfi;
894 nfi = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo));