NumberFormatInfo.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. //
  2. // System.Globalization.NumberFormatInfo.cs
  3. //
  4. // Author:
  5. // Derek Holden ([email protected])
  6. // Bob Smith ([email protected])
  7. // Mohammad DAMT ([email protected])
  8. //
  9. // (C) Derek Holden
  10. // (C) Bob Smith http://www.thestuff.net
  11. // (c) 2003, PT Cakram Datalingga Duaribu http://www.cdl2000.com
  12. //
  13. //
  14. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  15. //
  16. // Permission is hereby granted, free of charge, to any person obtaining
  17. // a copy of this software and associated documentation files (the
  18. // "Software"), to deal in the Software without restriction, including
  19. // without limitation the rights to use, copy, modify, merge, publish,
  20. // distribute, sublicense, and/or sell copies of the Software, and to
  21. // permit persons to whom the Software is furnished to do so, subject to
  22. // the following conditions:
  23. //
  24. // The above copyright notice and this permission notice shall be
  25. // included in all copies or substantial portions of the Software.
  26. //
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  28. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  29. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  30. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  31. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  32. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  33. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  34. //
  35. //
  36. // NumberFormatInfo. One can only assume it is the class gotten
  37. // back from a GetFormat() method from an IFormatProvider /
  38. // IFormattable implementer. There are some discrepencies with the
  39. // ECMA spec and the SDK docs, surprisingly. See my conversation
  40. // with myself on it at:
  41. // http://lists.ximian.com/archives/public/mono-list/2001-July/000794.html
  42. //
  43. // Other than that this is totally ECMA compliant.
  44. //
  45. namespace System.Globalization {
  46. [Serializable]
  47. public sealed class NumberFormatInfo : ICloneable, IFormatProvider {
  48. private bool isReadOnly;
  49. // used for temporary storage. Used in InitPatterns ()
  50. string decimalFormats;
  51. string currencyFormats;
  52. string percentFormats;
  53. string digitPattern = "#";
  54. string zeroPattern = "0";
  55. // Currency Related Format Info
  56. private int currencyDecimalDigits;
  57. private string currencyDecimalSeparator;
  58. private string currencyGroupSeparator;
  59. private int[] currencyGroupSizes;
  60. private int currencyNegativePattern;
  61. private int currencyPositivePattern;
  62. private string currencySymbol;
  63. private string nanSymbol;
  64. private string negativeInfinitySymbol;
  65. private string negativeSign;
  66. // Number Related Format Info
  67. private int numberDecimalDigits;
  68. private string numberDecimalSeparator;
  69. private string numberGroupSeparator;
  70. private int[] numberGroupSizes;
  71. private int numberNegativePattern;
  72. // Percent Related Format Info
  73. private int percentDecimalDigits;
  74. private string percentDecimalSeparator;
  75. private string percentGroupSeparator;
  76. private int[] percentGroupSizes;
  77. private int percentNegativePattern;
  78. private int percentPositivePattern;
  79. private string percentSymbol;
  80. private string perMilleSymbol;
  81. private string positiveInfinitySymbol;
  82. private string positiveSign;
  83. string ansiCurrencySymbol; // TODO, MS.NET serializes this.
  84. int m_dataItem; // Unused, but MS.NET serializes this.
  85. bool m_useUserOverride; // Unused, but MS.NET serializes this.
  86. bool validForParseAsNumber; // Unused, but MS.NET serializes this.
  87. bool validForParseAsCurrency; // Unused, but MS.NET serializes this.
  88. internal NumberFormatInfo (int lcid)
  89. {
  90. //FIXME: should add more LCID
  91. // CultureInfo uses this one also.
  92. if (lcid != 0x007F)
  93. lcid = 0x007F;
  94. switch (lcid){
  95. // The Invariant Culture Info ID.
  96. case 0x007f:
  97. isReadOnly = false;
  98. // Currency Related Format Info
  99. currencyDecimalDigits = 2;
  100. currencyDecimalSeparator = ".";
  101. currencyGroupSeparator = ",";
  102. currencyGroupSizes = new int[1] { 3 };
  103. currencyNegativePattern = 0;
  104. currencyPositivePattern = 0;
  105. currencySymbol = "$";
  106. nanSymbol = "NaN";
  107. negativeInfinitySymbol = "-Infinity";
  108. negativeSign = "-";
  109. // Number Related Format Info
  110. numberDecimalDigits = 2;
  111. numberDecimalSeparator = ".";
  112. numberGroupSeparator = ",";
  113. numberGroupSizes = new int[1] { 3 };
  114. numberNegativePattern = 1;
  115. // Percent Related Format Info
  116. percentDecimalDigits = 2;
  117. percentDecimalSeparator = ".";
  118. percentGroupSeparator = ",";
  119. percentGroupSizes = new int[1] { 3 };
  120. percentNegativePattern = 0;
  121. percentPositivePattern = 0;
  122. percentSymbol= "%";
  123. perMilleSymbol = "\u2030";
  124. positiveInfinitySymbol = "Infinity";
  125. positiveSign = "+";
  126. break;
  127. }
  128. }
  129. public NumberFormatInfo () : this (0x007f)
  130. {
  131. }
  132. // this is called by mono/mono/metadata/locales.c
  133. void InitPatterns ()
  134. {
  135. string [] partOne, partTwo;
  136. string [] posNeg = decimalFormats.Split (new char [1] {';'}, 2);
  137. if (posNeg.Length == 2) {
  138. partOne = posNeg [0].Split (new char [1] {'.'}, 2);
  139. if (partOne.Length == 2) {
  140. // assumed same for both positive and negative
  141. // decimal digit side
  142. numberDecimalDigits = 0;
  143. for (int i = 0; i < partOne [1].Length; i ++) {
  144. if (partOne [1][i] == digitPattern [0]) {
  145. numberDecimalDigits ++;
  146. } else
  147. break;
  148. }
  149. // decimal grouping side
  150. partTwo = partOne [0].Split (',');
  151. if (partTwo.Length > 1) {
  152. numberGroupSizes = new int [partTwo.Length - 1];
  153. for (int i = 0; i < numberGroupSizes.Length; i ++) {
  154. string pat = partTwo [i + 1];
  155. numberGroupSizes [i] = pat.Length;
  156. }
  157. } else {
  158. numberGroupSizes = new int [1] { 0 };
  159. }
  160. if (posNeg [1].StartsWith ("(") && posNeg [1].EndsWith (")")) {
  161. numberNegativePattern = 0;
  162. } else if (posNeg [1].StartsWith ("- ")) {
  163. numberNegativePattern = 2;
  164. } else if (posNeg [1].StartsWith ("-")) {
  165. numberNegativePattern = 1;
  166. } else if (posNeg [1].EndsWith (" -")) {
  167. numberNegativePattern = 4;
  168. } else if (posNeg [1].EndsWith ("-")) {
  169. numberNegativePattern = 3;
  170. } else {
  171. numberNegativePattern = 1;
  172. }
  173. }
  174. }
  175. posNeg = currencyFormats.Split (new char [1] {';'}, 2);
  176. if (posNeg.Length == 2) {
  177. partOne = posNeg [0].Split (new char [1] {'.'}, 2);
  178. if (partOne.Length == 2) {
  179. // assumed same for both positive and negative
  180. // decimal digit side
  181. currencyDecimalDigits = 0;
  182. for (int i = 0; i < partOne [1].Length; i ++) {
  183. if (partOne [1][i] == zeroPattern [0])
  184. currencyDecimalDigits ++;
  185. else
  186. break;
  187. }
  188. // decimal grouping side
  189. partTwo = partOne [0].Split (',');
  190. if (partTwo.Length > 1) {
  191. currencyGroupSizes = new int [partTwo.Length - 1];
  192. for (int i = 0; i < currencyGroupSizes.Length; i ++) {
  193. string pat = partTwo [i + 1];
  194. currencyGroupSizes [i] = pat.Length;
  195. }
  196. } else {
  197. currencyGroupSizes = new int [1] { 0 };
  198. }
  199. if (posNeg [1].StartsWith ("(\u00a4 ") && posNeg [1].EndsWith (")")) {
  200. currencyNegativePattern = 14;
  201. } else if (posNeg [1].StartsWith ("(\u00a4") && posNeg [1].EndsWith (")")) {
  202. currencyNegativePattern = 0;
  203. } else if (posNeg [1].StartsWith ("\u00a4 ") && posNeg [1].EndsWith ("-")) {
  204. currencyNegativePattern = 11;
  205. } else if (posNeg [1].StartsWith ("\u00a4") && posNeg [1].EndsWith ("-")) {
  206. currencyNegativePattern = 3;
  207. } else if (posNeg [1].StartsWith ("(") && posNeg [1].EndsWith (" \u00a4")) {
  208. currencyNegativePattern = 15;
  209. } else if (posNeg [1].StartsWith ("(") && posNeg [1].EndsWith ("\u00a4")) {
  210. currencyNegativePattern = 4;
  211. } else if (posNeg [1].StartsWith ("-") && posNeg [1].EndsWith (" \u00a4")) {
  212. currencyNegativePattern = 8;
  213. } else if (posNeg [1].StartsWith ("-") && posNeg [1].EndsWith ("\u00a4")) {
  214. currencyNegativePattern = 5;
  215. } else if (posNeg [1].StartsWith ("-\u00a4 ")) {
  216. currencyNegativePattern = 9;
  217. } else if (posNeg [1].StartsWith ("-\u00a4")) {
  218. currencyNegativePattern = 1;
  219. } else if (posNeg [1].StartsWith ("\u00a4 -")) {
  220. currencyNegativePattern = 12;
  221. } else if (posNeg [1].StartsWith ("\u00a4-")) {
  222. currencyNegativePattern = 2;
  223. } else if (posNeg [1].EndsWith (" \u00a4-")) {
  224. currencyNegativePattern = 10;
  225. } else if (posNeg [1].EndsWith ("\u00a4-")) {
  226. currencyNegativePattern = 7;
  227. } else if (posNeg [1].EndsWith ("- \u00a4")) {
  228. currencyNegativePattern = 13;
  229. } else if (posNeg [1].EndsWith ("-\u00a4")) {
  230. currencyNegativePattern = 6;
  231. } else {
  232. currencyNegativePattern = 0;
  233. }
  234. if (posNeg [0].StartsWith ("\u00a4 ")) {
  235. currencyPositivePattern = 2;
  236. } else if (posNeg [0].StartsWith ("\u00a4")) {
  237. currencyPositivePattern = 0;
  238. } else if (posNeg [0].EndsWith (" \u00a4")) {
  239. currencyPositivePattern = 3;
  240. } else if (posNeg [0].EndsWith ("\u00a4")) {
  241. currencyPositivePattern = 1;
  242. } else {
  243. currencyPositivePattern = 0;
  244. }
  245. }
  246. }
  247. // we don't have percentNegativePattern in CLDR so
  248. // the percentNegativePattern are just guesses
  249. if (percentFormats.StartsWith ("%")) {
  250. percentPositivePattern = 2;
  251. percentNegativePattern = 2;
  252. } else if (percentFormats.EndsWith (" %")) {
  253. percentPositivePattern = 0;
  254. percentNegativePattern = 0;
  255. } else if (percentFormats.EndsWith ("%")) {
  256. percentPositivePattern = 1;
  257. percentNegativePattern = 1;
  258. } else {
  259. percentPositivePattern = 0;
  260. percentNegativePattern = 0;
  261. }
  262. partOne = percentFormats.Split (new char [1] {'.'}, 2);
  263. if (partOne.Length == 2) {
  264. // assumed same for both positive and negative
  265. // decimal digit side
  266. percentDecimalDigits = 0;
  267. for (int i = 0; i < partOne [1].Length; i ++) {
  268. if (partOne [1][i] == digitPattern [0])
  269. percentDecimalDigits ++;
  270. else
  271. break;
  272. }
  273. // percent grouping side
  274. partTwo = partOne [0].Split (',');
  275. if (partTwo.Length > 1) {
  276. percentGroupSizes = new int [partTwo.Length - 1];
  277. for (int i = 0; i < percentGroupSizes.Length; i ++) {
  278. string pat = partTwo [i + 1];
  279. percentGroupSizes [i] = pat.Length;
  280. }
  281. } else {
  282. percentGroupSizes = new int [1] { 0 };
  283. }
  284. }
  285. }
  286. // =========== Currency Format Properties =========== //
  287. public int CurrencyDecimalDigits {
  288. get {
  289. return currencyDecimalDigits;
  290. }
  291. set {
  292. if (value < 0 || value > 99)
  293. throw new ArgumentOutOfRangeException
  294. ("The value specified for the property is less than 0 or greater than 99");
  295. if (isReadOnly)
  296. throw new InvalidOperationException
  297. ("The current instance is read-only and a set operation was attempted");
  298. currencyDecimalDigits = value;
  299. }
  300. }
  301. public string CurrencyDecimalSeparator {
  302. get {
  303. return currencyDecimalSeparator;
  304. }
  305. set {
  306. if (value == null)
  307. throw new ArgumentNullException
  308. ("The value specified for the property is a null reference");
  309. if (isReadOnly)
  310. throw new InvalidOperationException
  311. ("The current instance is read-only and a set operation was attempted");
  312. currencyDecimalSeparator = value;
  313. }
  314. }
  315. public string CurrencyGroupSeparator {
  316. get {
  317. return currencyGroupSeparator;
  318. }
  319. set {
  320. if (value == null)
  321. throw new ArgumentNullException
  322. ("The value specified for the property is a null reference");
  323. if (isReadOnly)
  324. throw new InvalidOperationException
  325. ("The current instance is read-only and a set operation was attempted");
  326. currencyGroupSeparator = value;
  327. }
  328. }
  329. public int[] CurrencyGroupSizes {
  330. get {
  331. return (int []) currencyGroupSizes.Clone ();
  332. }
  333. set {
  334. if (value == null || value.Length == 0)
  335. throw new ArgumentNullException
  336. ("The value specified for the property is a null reference");
  337. if (isReadOnly)
  338. throw new InvalidOperationException
  339. ("The current instance is read-only and a set operation was attempted");
  340. // All elements except last need to be in range 1 - 9, last can be 0.
  341. int last = value.Length - 1;
  342. for (int i = 0; i < last; i++)
  343. if (value[i] < 1 || value[i] > 9)
  344. throw new ArgumentOutOfRangeException
  345. ("One of the elements in the array specified is not between 1 and 9");
  346. if (value[last] < 0 || value[last] > 9)
  347. throw new ArgumentOutOfRangeException
  348. ("Last element in the array specified is not between 0 and 9");
  349. currencyGroupSizes = (int[]) value.Clone();
  350. }
  351. }
  352. public int CurrencyNegativePattern {
  353. get {
  354. // See ECMA NumberFormatInfo page 8
  355. return currencyNegativePattern;
  356. }
  357. set {
  358. if (value < 0 || value > 15)
  359. throw new ArgumentOutOfRangeException
  360. ("The value specified for the property is less than 0 or greater than 15");
  361. if (isReadOnly)
  362. throw new InvalidOperationException
  363. ("The current instance is read-only and a set operation was attempted");
  364. currencyNegativePattern = value;
  365. }
  366. }
  367. public int CurrencyPositivePattern {
  368. get {
  369. // See ECMA NumberFormatInfo page 11
  370. return currencyPositivePattern;
  371. }
  372. set {
  373. if (value < 0 || value > 3)
  374. throw new ArgumentOutOfRangeException
  375. ("The value specified for the property is less than 0 or greater than 3");
  376. if (isReadOnly)
  377. throw new InvalidOperationException
  378. ("The current instance is read-only and a set operation was attempted");
  379. currencyPositivePattern = value;
  380. }
  381. }
  382. public string CurrencySymbol {
  383. get {
  384. return currencySymbol;
  385. }
  386. set {
  387. if (value == null)
  388. throw new ArgumentNullException
  389. ("The value specified for the property is a null reference");
  390. if (isReadOnly)
  391. throw new InvalidOperationException
  392. ("The current instance is read-only and a set operation was attempted");
  393. currencySymbol = value;
  394. }
  395. }
  396. // =========== Static Read-Only Properties =========== //
  397. public static NumberFormatInfo CurrentInfo {
  398. get {
  399. NumberFormatInfo nfi = (NumberFormatInfo) System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat;
  400. nfi.isReadOnly = true;
  401. return nfi;
  402. }
  403. }
  404. public static NumberFormatInfo InvariantInfo {
  405. get {
  406. // This uses invariant info, which is same as in the constructor
  407. NumberFormatInfo nfi = new NumberFormatInfo ();
  408. nfi.NumberNegativePattern = 1;
  409. nfi.isReadOnly = true;
  410. return nfi;
  411. }
  412. }
  413. public bool IsReadOnly {
  414. get {
  415. return isReadOnly;
  416. }
  417. }
  418. public string NaNSymbol {
  419. get {
  420. return nanSymbol;
  421. }
  422. set {
  423. if (value == null)
  424. throw new ArgumentNullException
  425. ("The value specified for the property is a null reference");
  426. if (isReadOnly)
  427. throw new InvalidOperationException
  428. ("The current instance is read-only and a set operation was attempted");
  429. nanSymbol = value;
  430. }
  431. }
  432. public string NegativeInfinitySymbol {
  433. get {
  434. return negativeInfinitySymbol;
  435. }
  436. set {
  437. if (value == null)
  438. throw new ArgumentNullException
  439. ("The value specified for the property is a null reference");
  440. if (isReadOnly)
  441. throw new InvalidOperationException
  442. ("The current instance is read-only and a set operation was attempted");
  443. negativeInfinitySymbol = value;
  444. }
  445. }
  446. public string NegativeSign {
  447. get {
  448. return negativeSign;
  449. }
  450. set {
  451. if (value == null)
  452. throw new ArgumentNullException
  453. ("The value specified for the property is a null reference");
  454. if (isReadOnly)
  455. throw new InvalidOperationException
  456. ("The current instance is read-only and a set operation was attempted");
  457. negativeSign = value;
  458. }
  459. }
  460. // =========== Number Format Properties =========== //
  461. public int NumberDecimalDigits {
  462. get {
  463. return numberDecimalDigits;
  464. }
  465. set {
  466. if (value < 0 || value > 99)
  467. throw new ArgumentOutOfRangeException
  468. ("The value specified for the property is less than 0 or greater than 99");
  469. if (isReadOnly)
  470. throw new InvalidOperationException
  471. ("The current instance is read-only and a set operation was attempted");
  472. numberDecimalDigits = value;
  473. }
  474. }
  475. public string NumberDecimalSeparator {
  476. get {
  477. return numberDecimalSeparator;
  478. }
  479. set {
  480. if (value == null)
  481. throw new ArgumentNullException
  482. ("The value specified for the property is a null reference");
  483. if (isReadOnly)
  484. throw new InvalidOperationException
  485. ("The current instance is read-only and a set operation was attempted");
  486. numberDecimalSeparator = value;
  487. }
  488. }
  489. public string NumberGroupSeparator {
  490. get {
  491. return numberGroupSeparator;
  492. }
  493. set {
  494. if (value == null)
  495. throw new ArgumentNullException
  496. ("The value specified for the property is a null reference");
  497. if (isReadOnly)
  498. throw new InvalidOperationException
  499. ("The current instance is read-only and a set operation was attempted");
  500. numberGroupSeparator = value;
  501. }
  502. }
  503. public int[] NumberGroupSizes {
  504. get {
  505. return (int []) numberGroupSizes.Clone ();
  506. }
  507. set {
  508. if (value == null || value.Length == 0)
  509. throw new ArgumentNullException
  510. ("The value specified for the property is a null reference");
  511. if (isReadOnly)
  512. throw new InvalidOperationException
  513. ("The current instance is read-only and a set operation was attempted");
  514. // All elements except last need to be in range 1 - 9, last can be 0.
  515. int last = value.Length - 1;
  516. for (int i = 0; i < last; i++)
  517. if (value[i] < 1 || value[i] > 9)
  518. throw new ArgumentOutOfRangeException
  519. ("One of the elements in the array specified is not between 1 and 9");
  520. if (value[last] < 0 || value[last] > 9)
  521. throw new ArgumentOutOfRangeException
  522. ("Last element in the array specified is not between 0 and 9");
  523. numberGroupSizes = (int[]) value.Clone();
  524. }
  525. }
  526. public int NumberNegativePattern {
  527. get {
  528. // See ECMA NumberFormatInfo page 27
  529. return numberNegativePattern;
  530. }
  531. set {
  532. if (value < 0 || value > 4)
  533. throw new ArgumentOutOfRangeException
  534. ("The value specified for the property is less than 0 or greater than 15");
  535. if (isReadOnly)
  536. throw new InvalidOperationException
  537. ("The current instance is read-only and a set operation was attempted");
  538. numberNegativePattern = value;
  539. }
  540. }
  541. // =========== Percent Format Properties =========== //
  542. public int PercentDecimalDigits {
  543. get {
  544. return percentDecimalDigits;
  545. }
  546. set {
  547. if (value < 0 || value > 99)
  548. throw new ArgumentOutOfRangeException
  549. ("The value specified for the property is less than 0 or greater than 99");
  550. if (isReadOnly)
  551. throw new InvalidOperationException
  552. ("The current instance is read-only and a set operation was attempted");
  553. percentDecimalDigits = value;
  554. }
  555. }
  556. public string PercentDecimalSeparator {
  557. get {
  558. return percentDecimalSeparator;
  559. }
  560. set {
  561. if (value == null)
  562. throw new ArgumentNullException
  563. ("The value specified for the property is a null reference");
  564. if (isReadOnly)
  565. throw new InvalidOperationException
  566. ("The current instance is read-only and a set operation was attempted");
  567. percentDecimalSeparator = value;
  568. }
  569. }
  570. public string PercentGroupSeparator {
  571. get {
  572. return percentGroupSeparator;
  573. }
  574. set {
  575. if (value == null)
  576. throw new ArgumentNullException
  577. ("The value specified for the property is a null reference");
  578. if (isReadOnly)
  579. throw new InvalidOperationException
  580. ("The current instance is read-only and a set operation was attempted");
  581. percentGroupSeparator = value;
  582. }
  583. }
  584. public int[] PercentGroupSizes {
  585. get {
  586. return (int []) percentGroupSizes.Clone ();
  587. }
  588. set {
  589. if (value == null || value.Length == 0)
  590. throw new ArgumentNullException
  591. ("The value specified for the property is a null reference");
  592. if (isReadOnly)
  593. throw new InvalidOperationException
  594. ("The current instance is read-only and a set operation was attempted");
  595. if (this == CultureInfo.CurrentCulture.NumberFormat)
  596. throw new Exception ("HERE the value was modified");
  597. // All elements except last need to be in range 1 - 9, last can be 0.
  598. int last = value.Length - 1;
  599. for (int i = 0; i < last; i++)
  600. if (value[i] < 1 || value[i] > 9)
  601. throw new ArgumentOutOfRangeException
  602. ("One of the elements in the array specified is not between 1 and 9");
  603. if (value[last] < 0 || value[last] > 9)
  604. throw new ArgumentOutOfRangeException
  605. ("Last element in the array specified is not between 0 and 9");
  606. percentGroupSizes = (int[]) value.Clone();
  607. }
  608. }
  609. public int PercentNegativePattern {
  610. get {
  611. // See ECMA NumberFormatInfo page 8
  612. return percentNegativePattern;
  613. }
  614. set {
  615. if (value < 0 || value > 2)
  616. throw new ArgumentOutOfRangeException
  617. ("The value specified for the property is less than 0 or greater than 15");
  618. if (isReadOnly)
  619. throw new InvalidOperationException
  620. ("The current instance is read-only and a set operation was attempted");
  621. percentNegativePattern = value;
  622. }
  623. }
  624. public int PercentPositivePattern {
  625. get {
  626. // See ECMA NumberFormatInfo page 11
  627. return percentPositivePattern;
  628. }
  629. set {
  630. if (value < 0 || value > 2)
  631. throw new ArgumentOutOfRangeException
  632. ("The value specified for the property is less than 0 or greater than 3");
  633. if (isReadOnly)
  634. throw new InvalidOperationException
  635. ("The current instance is read-only and a set operation was attempted");
  636. percentPositivePattern = value;
  637. }
  638. }
  639. public string PercentSymbol {
  640. get {
  641. return percentSymbol;
  642. }
  643. set {
  644. if (value == null)
  645. throw new ArgumentNullException
  646. ("The value specified for the property is a null reference");
  647. if (isReadOnly)
  648. throw new InvalidOperationException
  649. ("The current instance is read-only and a set operation was attempted");
  650. percentSymbol = value;
  651. }
  652. }
  653. public string PerMilleSymbol {
  654. get {
  655. return perMilleSymbol;
  656. }
  657. set {
  658. if (value == null)
  659. throw new ArgumentNullException
  660. ("The value specified for the property is a null reference");
  661. if (isReadOnly)
  662. throw new InvalidOperationException
  663. ("The current instance is read-only and a set operation was attempted");
  664. perMilleSymbol = value;
  665. }
  666. }
  667. public string PositiveInfinitySymbol {
  668. get {
  669. return positiveInfinitySymbol;
  670. }
  671. set {
  672. if (value == null)
  673. throw new ArgumentNullException
  674. ("The value specified for the property is a null reference");
  675. if (isReadOnly)
  676. throw new InvalidOperationException
  677. ("The current instance is read-only and a set operation was attempted");
  678. positiveInfinitySymbol = value;
  679. }
  680. }
  681. public string PositiveSign {
  682. get {
  683. return positiveSign;
  684. }
  685. set {
  686. if (value == null)
  687. throw new ArgumentNullException
  688. ("The value specified for the property is a null reference");
  689. if (isReadOnly)
  690. throw new InvalidOperationException
  691. ("The current instance is read-only and a set operation was attempted");
  692. positiveSign = value;
  693. }
  694. }
  695. public object GetFormat (Type formatType)
  696. {
  697. return (formatType == typeof (NumberFormatInfo)) ? this : null;
  698. }
  699. public object Clone ()
  700. {
  701. NumberFormatInfo clone = (NumberFormatInfo) MemberwiseClone();
  702. // clone is not read only
  703. clone.isReadOnly = false;
  704. return clone;
  705. }
  706. public static NumberFormatInfo ReadOnly (NumberFormatInfo nfi)
  707. {
  708. NumberFormatInfo copy = (NumberFormatInfo)nfi.Clone();
  709. copy.isReadOnly = true;
  710. return copy;
  711. }
  712. public static NumberFormatInfo GetInstance(IFormatProvider provider)
  713. {
  714. if (provider != null) {
  715. NumberFormatInfo nfi;
  716. nfi = (NumberFormatInfo)provider.GetFormat(typeof(NumberFormatInfo));
  717. if (nfi != null)
  718. return nfi;
  719. }
  720. return CurrentInfo;
  721. }
  722. }
  723. }