Skip to content

Commit e35b03e

Browse files
authored
[class-parse] Use InvariantCulture to format floating-point values (#131)
Fixes: #129 The System.Double/Single ["Round-trip Format Specifier"][0] varies by culture, so cultures which use `,` as the decimal separator character use `,` within e.g. `42.0.ToString("r")`. For example, the Java declaration: class E { public static final double V = 42.5; } on french systems would result in `class-parse` emitting: <field name="V" value="42,5" .../> which, when run through `generator`, emits: public const double V = (double) 42,5; ...which promptly fails with a CS1001. Oops. Fix our `double` and `float` serialization in `XmlClassDeclarationBuilder` so that `CultureInfo.InvariantCulture` is used, which ensures that `.` is the decimal separator character, ensuring that `class-parse` and `generator` output is as expected. [0]: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx#RFormatString
1 parent 1d01fd3 commit e35b03e

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/Xamarin.Android.Tools.Bytecode/XmlClassDeclarationBuilder.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Globalization;
45
using System.Linq;
56
using System.Xml.Linq;
67
using System.Text;
@@ -443,9 +444,19 @@ static XAttribute GetValue (FieldInfo field)
443444
else if (Double.IsPositiveInfinity (doubleItem.Value))
444445
value = "(1.0 / 0.0)";
445446
else
446-
value = doubleItem.Value.ToString ("R");
447+
value = doubleItem.Value.ToString ("R", CultureInfo.InvariantCulture);
448+
break;
449+
case ConstantPoolItemType.Float:
450+
var floatItem = (ConstantPoolFloatItem) constant;
451+
if (Double.IsNaN (floatItem.Value))
452+
value = "(0.0f / 0.0f)";
453+
else if (Double.IsNegativeInfinity (floatItem.Value))
454+
value = "(-1.0f / 0.0f)";
455+
else if (Double.IsPositiveInfinity (floatItem.Value))
456+
value = "(1.0f / 0.0f)";
457+
else
458+
value = floatItem.Value.ToString ("R", CultureInfo.InvariantCulture);
447459
break;
448-
case ConstantPoolItemType.Float: value = ((ConstantPoolFloatItem)constant).Value.ToString ("R"); break;
449460
case ConstantPoolItemType.Long: value = ((ConstantPoolLongItem) constant).Value.ToString (); break;
450461
case ConstantPoolItemType.Integer:
451462
if (field.Descriptor == "Z")

0 commit comments

Comments
 (0)