1 // DoubleTest.cs - NUnit Test Cases for the System.Double class
3 // Bob Doan <bdoan@sicompos.com>
5 // (C) Ximian, Inc. http://www.ximian.com
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 using System.Collections;
11 using System.Globalization;
12 using System.Threading;
14 using NUnit.Framework;
16 namespace MonoTests.System
19 public class DoubleTest
21 private const Double d_zero = 0.0;
22 private const Double d_neg = -1234.5678;
23 private const Double d_pos = 1234.9999;
24 private const Double d_pos2 = 1234.9999;
25 private const Double d_nan = Double.NaN;
26 private const Double d_pinf = Double.PositiveInfinity;
27 private const Double d_ninf = Double.NegativeInfinity;
28 private const String s = "What Ever";
29 private NumberFormatInfo Nfi = NumberFormatInfo.InvariantInfo;
31 private string [] string_values;
32 private string [] string_values_fail = {
34 "- 1.0", // Inner whitespace
35 "3 5" // Inner whitespace 2
38 private double [] double_values = {
39 1, .1, 1.1, -12, 44.444432, .000021121,
40 .00001, .223, -221.3233,
41 1.7976931348623157e308, 1.7976931348623157e308, -1.7976931348623157e308,
42 4.9406564584124650e-324,
43 6.28318530717958647692528676655900577,
51 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
52 string_values = new string [15];
53 string_values [0] = "1";
54 string_values [1] = sep + "1";
55 string_values [2] = "1" + sep + "1";
56 string_values [3] = "-12";
57 string_values [4] = "44" + sep + "444432";
58 string_values [5] = sep + "000021121";
59 string_values [6] = " " + sep + "00001";
60 string_values [7] = " " + sep + "223 ";
61 string_values [8] = " -221" + sep + "3233";
62 string_values [9] = " 1" + sep + "7976931348623157e308 ";
63 string_values [10] = "+1" + sep + "7976931348623157E308";
64 string_values [11] = "-1" + sep + "7976931348623157e308";
65 string_values [12] = "4" + sep + "9406564584124650e-324";
66 string_values [13] = "6" + sep + "28318530717958647692528676655900577";
67 string_values [14] = "1e-05";
71 public void PublicFields ()
73 Assert.AreEqual (3.9406564584124654e-324, Double.Epsilon, "#1");
74 Assert.AreEqual (1.7976931348623157e+308, Double.MaxValue, "#2");
75 Assert.AreEqual (-1.7976931348623157e+308, Double.MinValue, "#3");
76 Assert.AreEqual ((double) -1.0 / (double) (0.0), Double.NegativeInfinity, "#4");
77 Assert.AreEqual ((double) 1.0 / (double) (0.0), Double.PositiveInfinity, "#5");
81 public void CompareTo ()
83 //If you do int foo = d_ninf.CompareTo(d_pinf); Assertion.Assert(".." foo < 0, true) this works.... WHY???
84 Assert.IsTrue (d_ninf.CompareTo (d_pinf) < 0, "#A1");
85 Assert.IsTrue (d_neg.CompareTo (d_pos) < 0, "#A2");
86 Assert.IsTrue (d_nan.CompareTo (d_neg) < 0, "#A3");
88 Assert.AreEqual (0, d_pos.CompareTo (d_pos2), "#B1");
89 Assert.AreEqual (0, d_pinf.CompareTo (d_pinf), "#B2");
90 Assert.AreEqual (0, d_ninf.CompareTo (d_ninf), "#B3");
91 Assert.AreEqual (0, d_nan.CompareTo (d_nan), "#B4");
93 Assert.IsTrue (d_pos.CompareTo (d_neg) > 0, "#C1");
94 Assert.IsTrue (d_pos.CompareTo (d_nan) > 0, "#C2");
95 Assert.IsTrue (d_pos.CompareTo (null) > 0, "#C3");
100 } catch (ArgumentException ex) {
101 Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#D2");
106 public void Equals ()
108 Assert.IsTrue (d_pos.Equals (d_pos2), "#1");
109 Assert.IsFalse (d_pos.Equals (d_neg), "#2");
110 Assert.IsFalse (d_pos.Equals (s), "#3");
114 public void TestTypeCode ()
116 Assert.AreEqual (TypeCode.Double, d_pos.GetTypeCode ());
120 public void IsInfinity ()
122 Assert.IsTrue (Double.IsInfinity (Double.PositiveInfinity), "#1");
123 Assert.IsTrue (Double.IsInfinity (Double.NegativeInfinity), "#2");
124 Assert.IsFalse (Double.IsInfinity (12), "#3");
130 Assert.IsTrue (Double.IsNaN (Double.NaN), "#1");
131 Assert.IsFalse (Double.IsNaN (12), "#2");
132 Assert.IsFalse (Double.IsNaN (Double.PositiveInfinity), "#3");
136 public void IsNegativeInfinity ()
138 Assert.IsTrue (Double.IsNegativeInfinity (Double.NegativeInfinity), "#1");
139 Assert.IsFalse (Double.IsNegativeInfinity (12), "#2");
143 public void IsPositiveInfinity ()
145 Assert.IsTrue (Double.IsPositiveInfinity (Double.PositiveInfinity), "#1");
146 Assert.IsFalse (Double.IsPositiveInfinity (12), "#2");
150 [SetCulture ("en-US")]
155 for (i = 0; i < string_values.Length; i++) {
156 Assert.AreEqual (double_values [i], Double.Parse (string_values [i]), "#A1");
158 } catch (Exception e) {
159 Assert.Fail ("#A2: i=" + i + " failed with e = " + e.ToString ());
163 Assert.AreEqual (10.1111, Double.Parse (" 10.1111 ", NumberStyles.Float, Nfi), "#B1");
164 } catch (Exception e) {
165 Assert.Fail ("#B2: Parse Failed NumberStyles.Float with e = " + e.ToString ());
169 Assert.AreEqual (1234.5678, Double.Parse ("1,234.5678", NumberStyles.Float | NumberStyles.AllowThousands, Nfi), "#C1");
170 } catch (Exception e) {
171 Assert.Fail ("#C2: Parse Failed NumberStyles.AllowThousands with e = " + e.ToString ());
177 } catch (ArgumentNullException ex) {
178 Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#D2");
179 Assert.IsNull (ex.InnerException, "#D3");
180 Assert.IsNotNull (ex.Message, "#D4");
181 Assert.IsNotNull (ex.ParamName, "#D5");
185 Double.Parse ("save the elk");
187 } catch (FormatException ex) {
188 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#E2");
189 Assert.IsNull (ex.InnerException, "#E3");
190 Assert.IsNotNull (ex.Message, "#E4");
193 string sep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator;
196 ovf_plus = Double.Parse ("1" + sep + "79769313486232e308");
198 } catch (OverflowException ex) {
199 Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#F2");
200 Assert.IsNull (ex.InnerException, "#F3");
201 Assert.IsNotNull (ex.Message, "#F4");
205 Double.Parse ("-1" + sep + "79769313486232e308");
207 } catch (OverflowException ex) {
208 Assert.AreEqual (typeof (OverflowException), ex.GetType (), "#G2");
209 Assert.IsNull (ex.InnerException, "#G3");
210 Assert.IsNotNull (ex.Message, "#G4");
213 for (i = 0; i < string_values_fail.Length; ++i) {
215 Double.Parse (string_values_fail [i]);
216 Assert.Fail ("#H1: " + string_values_fail [i]);
217 } catch (FormatException ex) {
218 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#H2");
219 Assert.IsNull (ex.InnerException, "#H3");
220 Assert.IsNotNull (ex.Message, "#H4");
226 public void ParseAllowWhitespaces ()
228 var nf = CultureInfo.CurrentCulture.NumberFormat;
229 NumberStyles style = NumberStyles.Float;
230 double.Parse (" 32 ");
231 double.Parse (string.Format (" {0} ", nf.PositiveInfinitySymbol));
232 double.Parse (string.Format (" {0} ", nf.NegativeInfinitySymbol));
233 double.Parse (string.Format (" {0} ", nf.NaNSymbol));
237 public void Parse_Whitespace ()
242 } catch (FormatException ex) {
243 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2");
244 Assert.IsNull (ex.InnerException, "#3");
245 Assert.IsNotNull (ex.Message, "#4");
249 [Test] // //bug #81777
250 public void Parse_TrailingGarbage ()
253 double.Parse ("22 foo");
255 } catch (FormatException ex) {
256 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#2");
257 Assert.IsNull (ex.InnerException, "#3");
258 Assert.IsNotNull (ex.Message, "#4");
263 public void Parse_Infinity ()
266 IFormatProvider german = new CultureInfo ("de-DE");
267 var res = double.Parse ("+unendlich", NumberStyles.Float, german);
268 Assert.AreEqual (double.PositiveInfinity, res);
272 public void TestToString ()
278 } catch (FormatException ex) {
279 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#A2");
280 Assert.IsNull (ex.InnerException, "#A3");
281 Assert.IsNotNull (ex.Message, "#A4");
288 } catch (FormatException ex) {
289 Assert.AreEqual (typeof (FormatException), ex.GetType (), "#B2");
290 Assert.IsNull (ex.InnerException, "#B3");
291 Assert.IsNotNull (ex.Message, "#B4");
294 CustomFormatHelper ();
297 private class Element
300 public string format;
301 public string result;
303 public Element (double value, string format, string result)
306 this.format = format;
307 this.result = result;
311 public void CustomFormatHelper ()
313 NumberFormatInfo nfi = new NumberFormatInfo ();
315 nfi.NaNSymbol = "NaN";
316 nfi.PositiveSign = "+";
317 nfi.NegativeSign = "-";
318 nfi.PerMilleSymbol = "x";
319 nfi.PositiveInfinitySymbol = "Infinity";
320 nfi.NegativeInfinitySymbol = "-Infinity";
322 nfi.NumberDecimalDigits = 5;
323 nfi.NumberDecimalSeparator = ".";
324 nfi.NumberGroupSeparator = ",";
325 nfi.NumberGroupSizes = new int [] { 3 };
326 nfi.NumberNegativePattern = 2;
328 nfi.CurrencyDecimalDigits = 2;
329 nfi.CurrencyDecimalSeparator = ".";
330 nfi.CurrencyGroupSeparator = ",";
331 nfi.CurrencyGroupSizes = new int [] { 3 };
332 nfi.CurrencyNegativePattern = 8;
333 nfi.CurrencyPositivePattern = 3;
334 nfi.CurrencySymbol = "$";
336 nfi.PercentDecimalDigits = 5;
337 nfi.PercentDecimalSeparator = ".";
338 nfi.PercentGroupSeparator = ",";
339 nfi.PercentGroupSizes = new int [] { 3 };
340 nfi.PercentNegativePattern = 0;
341 nfi.PercentPositivePattern = 0;
342 nfi.PercentSymbol = "%";
344 ArrayList list = new ArrayList ();
345 list.Add (new Element (123d, "#####", "123"));
346 list.Add (new Element (123d, "00000", "00123"));
347 list.Add (new Element (123d, "(###) ### - ####", "() - 123"));
348 list.Add (new Element (123d, "#.##", "123"));
349 list.Add (new Element (123d, "0.00", "123.00"));
350 list.Add (new Element (123d, "00.00", "123.00"));
351 list.Add (new Element (123d, "#,#", "123"));
352 list.Add (new Element (123d, "#,,", ""));
353 list.Add (new Element (123d, "#,,,", ""));
354 list.Add (new Element (123d, "#,##0,,", "0"));
355 list.Add (new Element (123d, "#0.##%", "12300%"));
356 list.Add (new Element (123d, "0.###E+0", "1.23E+2"));
357 list.Add (new Element (123d, "0.###E+000", "1.23E+002"));
358 list.Add (new Element (123d, "0.###E-000", "1.23E002"));
359 list.Add (new Element (123d, "[##-##-##]", "[-1-23]"));
360 list.Add (new Element (123d, "##;(##)", "123"));
361 list.Add (new Element (123d, "##;(##)", "123"));
362 list.Add (new Element (1234567890d, "#####", "1234567890"));
363 list.Add (new Element (1234567890d, "00000", "1234567890"));
364 list.Add (new Element (1234567890d,
365 "(###) ### - ####", "(123) 456 - 7890"));
366 list.Add (new Element (1234567890d, "#.##", "1234567890"));
367 list.Add (new Element (1234567890d, "0.00", "1234567890.00"));
368 list.Add (new Element (1234567890d, "00.00", "1234567890.00"));
369 list.Add (new Element (1234567890d, "#,#", "1,234,567,890"));
370 list.Add (new Element (1234567890d, "#,,", "1235"));
371 list.Add (new Element (1234567890d, "#,,,", "1"));
372 list.Add (new Element (1234567890d, "#,##0,,", "1,235"));
373 list.Add (new Element (1234567890d, "#0.##%", "123456789000%"));
374 list.Add (new Element (1234567890d, "0.###E+0", "1.235E+9"));
375 list.Add (new Element (1234567890d, "0.###E+000", "1.235E+009"));
376 list.Add (new Element (1234567890d, "0.###E-000", "1.235E009"));
377 list.Add (new Element (1234567890d, "[##-##-##]", "[123456-78-90]"));
378 list.Add (new Element (1234567890d, "##;(##)", "1234567890"));
379 list.Add (new Element (1234567890d, "##;(##)", "1234567890"));
380 list.Add (new Element (1.2d, "#####", "1"));
381 list.Add (new Element (1.2d, "00000", "00001"));
382 list.Add (new Element (1.2d, "(###) ### - ####", "() - 1"));
383 list.Add (new Element (1.2d, "#.##", "1.2"));
384 list.Add (new Element (1.2d, "0.00", "1.20"));
385 list.Add (new Element (1.2d, "00.00", "01.20"));
386 list.Add (new Element (1.2d, "#,#", "1"));
387 list.Add (new Element (1.2d, "#,,", ""));
388 list.Add (new Element (1.2d, "#,,,", ""));
389 list.Add (new Element (1.2d, "#,##0,,", "0"));
390 list.Add (new Element (1.2d, "#0.##%", "120%"));
391 list.Add (new Element (1.2d, "0.###E+0", "1.2E+0"));
392 list.Add (new Element (1.2d, "0.###E+000", "1.2E+000"));
393 list.Add (new Element (1.2d, "0.###E-000", "1.2E000"));
394 list.Add (new Element (1.2d, "[##-##-##]", "[--1]"));
395 list.Add (new Element (1.2d, "##;(##)", "1"));
396 list.Add (new Element (1.2d, "##;(##)", "1"));
397 list.Add (new Element (0.086d, "#####", ""));
398 list.Add (new Element (0.086d, "00000", "00000"));
399 list.Add (new Element (0.086d, "(###) ### - ####", "() - "));
400 list.Add (new Element (0.086d, "#.##", ".09"));
401 list.Add (new Element (0.086d, "#.#", ".1"));
402 list.Add (new Element (0.086d, "0.00", "0.09"));
403 list.Add (new Element (0.086d, "00.00", "00.09"));
404 list.Add (new Element (0.086d, "#,#", ""));
405 list.Add (new Element (0.086d, "#,,", ""));
406 list.Add (new Element (0.086d, "#,,,", ""));
407 list.Add (new Element (0.086d, "#,##0,,", "0"));
408 list.Add (new Element (0.086d, "#0.##%", "8.6%"));
409 list.Add (new Element (0.086d, "0.###E+0", "8.6E-2"));
410 list.Add (new Element (0.086d, "0.###E+000", "8.6E-002"));
411 list.Add (new Element (0.086d, "0.###E-000", "8.6E-002"));
412 list.Add (new Element (0.086d, "[##-##-##]", "[--]"));
413 list.Add (new Element (0.086d, "##;(##)", ""));
414 list.Add (new Element (0.086d, "##;(##)", ""));
415 list.Add (new Element (86000d, "#####", "86000"));
416 list.Add (new Element (86000d, "00000", "86000"));
417 list.Add (new Element (86000d, "(###) ### - ####", "() 8 - 6000"));
418 list.Add (new Element (86000d, "#.##", "86000"));
419 list.Add (new Element (86000d, "0.00", "86000.00"));
420 list.Add (new Element (86000d, "00.00", "86000.00"));
421 list.Add (new Element (86000d, "#,#", "86,000"));
422 list.Add (new Element (86000d, "#,,", ""));
423 list.Add (new Element (86000d, "#,,,", ""));
424 list.Add (new Element (86000d, "#,##0,,", "0"));
425 list.Add (new Element (86000d, "#0.##%", "8600000%"));
426 list.Add (new Element (86000d, "0.###E+0", "8.6E+4"));
427 list.Add (new Element (86000d, "0.###E+000", "8.6E+004"));
428 list.Add (new Element (86000d, "0.###E-000", "8.6E004"));
429 list.Add (new Element (86000d, "[##-##-##]", "[8-60-00]"));
430 list.Add (new Element (86000d, "##;(##)", "86000"));
431 list.Add (new Element (86000d, "##;(##)", "86000"));
432 list.Add (new Element (123456d, "#####", "123456"));
433 list.Add (new Element (123456d, "00000", "123456"));
434 list.Add (new Element (123456d, "(###) ### - ####", "() 12 - 3456"));
435 list.Add (new Element (123456d, "#.##", "123456"));
436 list.Add (new Element (123456d, "0.00", "123456.00"));
437 list.Add (new Element (123456d, "00.00", "123456.00"));
438 list.Add (new Element (123456d, "#,#", "123,456"));
439 list.Add (new Element (123456d, "#,,", ""));
440 list.Add (new Element (123456d, "#,,,", ""));
441 list.Add (new Element (123456d, "#,##0,,", "0"));
442 list.Add (new Element (123456d, "#0.##%", "12345600%"));
443 list.Add (new Element (123456d, "0.###E+0", "1.235E+5"));
444 list.Add (new Element (123456d, "0.###E+000", "1.235E+005"));
445 list.Add (new Element (123456d, "0.###E-000", "1.235E005"));
446 list.Add (new Element (123456d, "[##-##-##]", "[12-34-56]"));
447 list.Add (new Element (123456d, "##;(##)", "123456"));
448 list.Add (new Element (123456d, "##;(##)", "123456"));
449 list.Add (new Element (1234d, "#####", "1234"));
450 list.Add (new Element (1234d, "00000", "01234"));
451 list.Add (new Element (1234d, "(###) ### - ####", "() - 1234"));
452 list.Add (new Element (1234d, "#.##", "1234"));
453 list.Add (new Element (1234d, "0.00", "1234.00"));
454 list.Add (new Element (1234d, "00.00", "1234.00"));
455 list.Add (new Element (1234d, "#,#", "1,234"));
456 list.Add (new Element (1234d, "#,,", ""));
457 list.Add (new Element (1234d, "#,,,", ""));
458 list.Add (new Element (1234d, "#,##0,,", "0"));
459 list.Add (new Element (1234d, "#0.##%", "123400%"));
460 list.Add (new Element (1234d, "0.###E+0", "1.234E+3"));
461 list.Add (new Element (1234d, "0.###E+000", "1.234E+003"));
462 list.Add (new Element (1234d, "0.###E-000", "1.234E003"));
463 list.Add (new Element (1234d, "[##-##-##]", "[-12-34]"));
464 list.Add (new Element (1234d, "##;(##)", "1234"));
465 list.Add (new Element (1234d, "##;(##)", "1234"));
466 list.Add (new Element (-1234d, "#####", "-1234"));
467 list.Add (new Element (-1234d, "00000", "-01234"));
468 list.Add (new Element (-1234d, "(###) ### - ####", "-() - 1234"));
469 list.Add (new Element (-1234d, "#.##", "-1234"));
470 list.Add (new Element (-1234d, "0.00", "-1234.00"));
471 list.Add (new Element (-1234d, "00.00", "-1234.00"));
472 list.Add (new Element (-1234d, "#,#", "-1,234"));
473 list.Add (new Element (-1234d, "#,,", ""));
474 list.Add (new Element (-1234d, "#,,,", ""));
475 list.Add (new Element (-1234d, "#,##0,,", "0"));
476 list.Add (new Element (-1234d, "#0.##%", "-123400%"));
477 list.Add (new Element (-1234d, "0.###E+0", "-1.234E+3"));
478 list.Add (new Element (-1234d, "0.###E+000", "-1.234E+003"));
479 list.Add (new Element (-1234d, "0.###E-000", "-1.234E003"));
480 list.Add (new Element (-1234d, "[##-##-##]", "-[-12-34]"));
481 list.Add (new Element (-1234d, "##;(##)", "(1234)"));
482 list.Add (new Element (-1234d, "##;(##)", "(1234)"));
483 list.Add (new Element (12345678901234567890.123d,
484 "#####", "12345678901234600000"));
485 list.Add (new Element (12345678901234567890.123d,
486 "00000", "12345678901234600000"));
487 list.Add (new Element (12345678901234567890.123d,
488 "(###) ### - ####", "(1234567890123) 460 - 0000"));
489 list.Add (new Element (12345678901234567890.123d,
490 "#.##", "12345678901234600000"));
491 list.Add (new Element (12345678901234567890.123d,
492 "0.00", "12345678901234600000.00"));
493 list.Add (new Element (12345678901234567890.123d,
494 "00.00", "12345678901234600000.00"));
495 list.Add (new Element (12345678901234567890.123d,
496 "#,#", "12,345,678,901,234,600,000"));
497 list.Add (new Element (12345678901234567890.123d,
498 "#,,", "12345678901235"));
499 list.Add (new Element (12345678901234567890.123d,
500 "#,,,", "12345678901"));
501 list.Add (new Element (12345678901234567890.123d,
502 "#,##0,,", "12,345,678,901,235"));
503 list.Add (new Element (12345678901234567890.123d,
504 "#0.##%", "1234567890123460000000%"));
505 list.Add (new Element (12345678901234567890.123d,
506 "0.###E+0", "1.235E+19"));
507 list.Add (new Element (12345678901234567890.123d,
508 "0.###E+000", "1.235E+019"));
509 list.Add (new Element (12345678901234567890.123d,
510 "0.###E-000", "1.235E019"));
511 list.Add (new Element (12345678901234567890.123d,
512 "[##-##-##]", "[1234567890123460-00-00]"));
513 list.Add (new Element (12345678901234567890.123d,
514 "##;(##)", "12345678901234600000"));
515 list.Add (new Element (12345678901234567890.123d,
516 "##;(##)", "12345678901234600000"));
517 foreach (Element e in list) {
518 Assert.AreEqual (e.result, e.value.ToString (e.format, nfi),
519 "ToString Failed: '" + e.value + "' Should be \"" + e.result + "\" with \"" + e.format + "\"");
524 public void ToString_Defaults ()
526 CultureInfo originalCulture = CultureInfo.CurrentCulture;
527 Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
530 // everything defaults to "G"
531 string def = i.ToString ("G");
532 Assert.AreEqual (def, i.ToString (), "#1");
533 Assert.AreEqual (def, i.ToString ((IFormatProvider) null), "#2");
534 Assert.AreEqual (def, i.ToString ((string) null), "#3");
535 Assert.AreEqual (def, i.ToString (String.Empty), "#4");
536 Assert.AreEqual (def, i.ToString (null, null), "#5");
537 Assert.AreEqual (def, i.ToString (String.Empty, null), "#6");
538 Assert.AreEqual ("254,9", def, "#7");
540 // restore original culture
541 Thread.CurrentThread.CurrentCulture = originalCulture;
546 public void TestRoundtrip () // bug #320433
548 Assert.AreEqual ("10.78", 10.78.ToString ("R", NumberFormatInfo.InvariantInfo));
552 public void LongLongValueRoundtrip ()
554 CultureInfo originalCulture = CultureInfo.CurrentCulture;
555 Thread.CurrentThread.CurrentCulture = new CultureInfo ("nl-BE");
557 double d = 0.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000222;
558 Assert.AreEqual ("1,97626258336499E-323", d.ToString ("R"));
560 // restore original culture
561 Thread.CurrentThread.CurrentCulture = originalCulture;
566 [ExpectedException (typeof (ArgumentException))]
567 public void HexNumber_WithHexToParse ()
571 Assert.IsTrue (Double.TryParse ("0dead", NumberStyles.HexNumber, null, out d), "#1");
572 Assert.AreEqual (57842, d, "#2");
576 [ExpectedException (typeof (ArgumentException))]
577 public void HexNumber_NoHexToParse ()
580 Assert.IsTrue (Double.TryParse ("0", NumberStyles.HexNumber, null, out d), "#1");
581 Assert.AreEqual (0, d, "#2");
585 public void TryParseBug78546 ()
588 Assert.IsFalse (Double.TryParse ("error", NumberStyles.Integer,
593 public void TryParse_NonDigitStrings ()
596 Assert.IsFalse (Double.TryParse ("string", NumberStyles.Any, null, out value), "#1");
597 Assert.IsFalse (Double.TryParse ("with whitespace", NumberStyles.Any, null, out value), "#2");
599 Assert.IsFalse (Double.TryParse ("string", out value), "#3");
600 Assert.IsFalse (Double.TryParse ("with whitespace", out value), "#4");
605 public void ParseCurrency ()
607 CultureInfo originalCulture = CultureInfo.CurrentCulture;
608 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
610 NumberFormatInfo f = NumberFormatInfo.CurrentInfo;
611 double d = double.Parse ("$4.56", NumberStyles.Currency, f);
612 Assert.AreEqual (4.56, d);
614 Thread.CurrentThread.CurrentCulture = originalCulture;
619 public void ParseEmptyNumberGroupSeparator ()
621 CultureInfo originalCulture = CultureInfo.CurrentCulture;
622 Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US");
624 var nf = new NumberFormatInfo ();
625 nf.NumberDecimalSeparator = ".";
626 nf.NumberGroupSeparator = "";
627 double d = double.Parse ("4.5", nf);
628 Assert.AreEqual (4.5, d);
630 Thread.CurrentThread.CurrentCulture = originalCulture;
635 public void ParseAdvanced ()
637 Assert.AreEqual (-456, double.Parse("(456 ) ", NumberStyles.AllowParentheses | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture), "#1");
638 Assert.AreEqual (-456, double.Parse("(456)", NumberStyles.AllowParentheses, CultureInfo.InvariantCulture), "#2");
639 Assert.AreEqual (-3, double.Parse("3-", NumberStyles.AllowTrailingSign, CultureInfo.InvariantCulture), "#3");
640 Assert.AreEqual (-3.5, double.Parse("3.5-", NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture), "#4");
641 Assert.AreEqual (-0.5, double.Parse ("0.5 - ", NumberStyles.AllowTrailingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowTrailingWhite, CultureInfo.InvariantCulture), "#5");
642 Assert.AreEqual (-900000, double.Parse ("-9E5 " + CultureInfo.InvariantCulture.NumberFormat.CurrencySymbol + " ", NumberStyles.Any, CultureInfo.InvariantCulture), "#6");