Skip to content

Commit

Permalink
[corlib] Decimal parsing with banker's rounding with non-zero digits …
Browse files Browse the repository at this point in the history
…beyond the thousandths digit. Fixes #17536
  • Loading branch information
marek-safar committed Jan 31, 2014
1 parent d64b215 commit a5fc91f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
26 changes: 14 additions & 12 deletions mcs/class/corlib/System/Decimal.cs
Expand Up @@ -4,14 +4,13 @@
// Represents a floating-point decimal data type with up to 29
// significant digits, suitable for financial and commercial calculations.
//
// Author:
// Authors:
// Martin Weindel (martin.weindel@t-online.de)
// Marek Safar (marek.safar@gmail.com)
//
// (C) 2001 Martin Weindel
//

//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
// Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
Expand Down Expand Up @@ -1081,22 +1080,25 @@ static bool PerformParse (string s, NumberStyles style, IFormatProvider provider
// then we trunc the string
if ((len > max) && (iDecPos < len)) {
int round = (s [max] - '0');
s = s.Substring (0, max);

bool addone = false;
if (round > 5) {
addone = true;
}
else if (round == 5) {
} else if (round == 5) {
if (isNegative) {
addone = true;
}
else {
// banker rounding applies :(
int previous = (s [max - 1] - '0');
addone = ((previous & 0x01) == 0x01);
} else {
// banker's rounding applies
if (len > max) {
addone = s [max + 1] > '0';
} else {
int previous = s [max - 1] - '0';
addone = ((previous & 0x01) == 0x01);
}
}
}

s = s.Substring (0, max);
if (addone) {
char[] array = s.ToCharArray ();
int p = max - 1;
Expand Down
4 changes: 3 additions & 1 deletion mcs/class/corlib/Test/System/DecimalTest.cs
Expand Up @@ -335,7 +335,9 @@ public void TestPercentPattern ()
new ParseTest("-000000000000001922816251426433759354395033.300000000000000", -1922816251426433759354395033.3m),
new ParseTest("-7922816251426433759354395033.150000000000", -7922816251426433759354395033.2m),
new ParseTest("-7922816251426433759354395033.2400000000000", -7922816251426433759354395033.2m),
new ParseTest("-7922816251426433759354395033.2600000000000", -7922816251426433759354395033.3m)
new ParseTest("-7922816251426433759354395033.2600000000000", -7922816251426433759354395033.3m),
new ParseTest("987654321098765432109876543.25999", 987654321098765432109876543.3m, NumberStyles.Float)

};

[Test]
Expand Down

0 comments on commit a5fc91f

Please sign in to comment.