NumberFormatInfo.cs 24 KB

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