Skip to content

Commit abf7f24

Browse files
Fix DecimalConverter to support scientific notation
- Changed NumberStyles from Number to Float | AllowThousands - This enables parsing of scientific notation values (e.g., 1.2345678E5) - Aligns DecimalConverter with SingleConverter and DoubleConverter behavior - Added comprehensive tests for scientific notation parsing Fixes #35 Co-authored-by: Chrissy LeMaire <[email protected]>
1 parent af724b2 commit abf7f24

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

project/dbatools.Tests/Csv/TypeConverterTest.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,26 @@ public void TestDecimalConverter()
161161
Assert.AreEqual(1234567890.123456m, result);
162162
}
163163

164+
[TestMethod]
165+
public void TestDecimalConverterScientificNotation()
166+
{
167+
var converter = DecimalConverter.Default;
168+
169+
// Test case from issue #35
170+
Assert.IsTrue(converter.TryConvert("1.2345678E5", out decimal result));
171+
Assert.AreEqual(123456.78m, result);
172+
173+
// Additional scientific notation tests
174+
Assert.IsTrue(converter.TryConvert("1.5e10", out result));
175+
Assert.AreEqual(15000000000m, result);
176+
177+
Assert.IsTrue(converter.TryConvert("2.5E-3", out result));
178+
Assert.AreEqual(0.0025m, result);
179+
180+
Assert.IsTrue(converter.TryConvert("-3.14E2", out result));
181+
Assert.AreEqual(-314m, result);
182+
}
183+
164184
#endregion
165185

166186
#region Type Converter Registry Tests

project/dbatools/Csv/TypeConverters/NumericConverters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ protected override bool TryParseSpan(ReadOnlySpan<char> value, NumberStyles styl
135135

136136
/// <summary>
137137
/// Converts string values to Decimal values.
138-
/// Supports culture-aware parsing for decimal separators.
138+
/// Supports culture-aware parsing for decimal separators and scientific notation.
139139
/// Addresses LumenWorks issue #66 for Czech locale decimal parsing.
140140
/// </summary>
141141
public sealed class DecimalConverter : CultureAwareConverterBase<decimal>
@@ -146,7 +146,7 @@ public sealed class DecimalConverter : CultureAwareConverterBase<decimal>
146146
/// <summary>Initializes a new instance of the <see cref="DecimalConverter"/> class.</summary>
147147
public DecimalConverter()
148148
{
149-
NumberStyles = NumberStyles.Number;
149+
NumberStyles = NumberStyles.Float | NumberStyles.AllowThousands;
150150
}
151151

152152
/// <inheritdoc />

0 commit comments

Comments
 (0)