Index: Core/Common/src/Core.Common.Gui/Converters/RoundDoubleToDecimalPlacesConverter.cs =================================================================== diff -u -r018134e6188262c4393c4d104a342c86e411081d -r6b7627d4f5d6fb52e4f3fda99508298be6cae2ff --- Core/Common/src/Core.Common.Gui/Converters/RoundDoubleToDecimalPlacesConverter.cs (.../RoundDoubleToDecimalPlacesConverter.cs) (revision 018134e6188262c4393c4d104a342c86e411081d) +++ Core/Common/src/Core.Common.Gui/Converters/RoundDoubleToDecimalPlacesConverter.cs (.../RoundDoubleToDecimalPlacesConverter.cs) (revision 6b7627d4f5d6fb52e4f3fda99508298be6cae2ff) @@ -47,22 +47,22 @@ { if (destinationType == typeof(double)) { - decimal numberAsDecimal = ConvertValueToDecimal(value); - decimal roundedDecimal = Math.Round(numberAsDecimal, numberOfDecimalPlaces); - return Convert.ToDouble(roundedDecimal); + return ConvertValueToRoundedDouble(value); } if (destinationType == typeof(string)) { - decimal numberAsDecimal = ConvertValueToDecimal(value); - decimal roundedDecimal = Math.Round(numberAsDecimal, numberOfDecimalPlaces); - return roundedDecimal.ToString(GetStringFormat()); + return ConvertValueToRoundedFixedPointString(value); } return base.ConvertTo(context, culture, value, destinationType); } - private string GetStringFormat() + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { - return "F"+numberOfDecimalPlaces; + if (value is double || value is string) + { + return ConvertValueToRoundedDouble(value); + } + return base.ConvertFrom(context, culture, value); } private static decimal ConvertValueToDecimal(object value) @@ -82,6 +82,29 @@ throw new NotSupportedException(message, overflowException); } } + + private decimal ConvertValueToRoundedDecimal(object value) + { + decimal numberAsDecimal = ConvertValueToDecimal(value); + return Math.Round(numberAsDecimal, numberOfDecimalPlaces); + } + + private object ConvertValueToRoundedDouble(object value) + { + var roundedDecimal = ConvertValueToRoundedDecimal(value); + return Convert.ToDouble(roundedDecimal); + } + + private object ConvertValueToRoundedFixedPointString(object value) + { + var roundedDecimal = ConvertValueToRoundedDecimal(value); + return roundedDecimal.ToString(GetStringFormat()); + } + + private string GetStringFormat() + { + return "F" + numberOfDecimalPlaces; + } } #region Specializations of RoundDoubleToDecimalPlacesConverter to be used for TypeConverterAttribute Index: Core/Common/test/Core.Common.Gui.Test/Converters/RoundDoubleToDecimalPlacesConverterTest.cs =================================================================== diff -u -r018134e6188262c4393c4d104a342c86e411081d -r6b7627d4f5d6fb52e4f3fda99508298be6cae2ff --- Core/Common/test/Core.Common.Gui.Test/Converters/RoundDoubleToDecimalPlacesConverterTest.cs (.../RoundDoubleToDecimalPlacesConverterTest.cs) (revision 018134e6188262c4393c4d104a342c86e411081d) +++ Core/Common/test/Core.Common.Gui.Test/Converters/RoundDoubleToDecimalPlacesConverterTest.cs (.../RoundDoubleToDecimalPlacesConverterTest.cs) (revision 6b7627d4f5d6fb52e4f3fda99508298be6cae2ff) @@ -340,5 +340,88 @@ string message = Assert.Throws(call).Message; Assert.AreEqual(string.Format("De waarde '{0}' is te groot of te klein om te kunnen verwerken.", input), message); } + + [Test] + [TestCase(1.0, 2, 1.00)] + [TestCase(123456789.0, 3, 123456789.000)] + [TestCase(12345678.90, 2, 12345678.90)] + [TestCase(12345678.90, 3, 12345678.900)] + [TestCase(1234567.890, 2, 1234567.89)] + [TestCase(1234567.890, 3, 1234567.890)] + [TestCase(123456.7890, 2, 123456.79)] + [TestCase(123456.7890, 3, 123456.789)] + [TestCase(12345.67890, 2, 12345.68)] + [TestCase(12345.67890, 3, 12345.679)] + [TestCase(1234.567890, 2, 1234.57)] + [TestCase(1234.567890, 3, 1234.568)] + [TestCase(123.4567890, 2, 123.46)] + [TestCase(123.4567890, 3, 123.457)] + [TestCase(12.34567890, 2, 12.35)] + [TestCase(12.34567890, 3, 12.346)] + [TestCase(1.234567890, 2, 1.23)] + [TestCase(1.234567890, 3, 1.235)] + [TestCase(0.1234567890, 2, 0.12)] + [TestCase(0.1234567890, 3, 0.123)] + [TestCase(0.01234567890, 2, 0.01)] + [TestCase(0.01234567890, 3, 0.012)] + [TestCase(0.001234567890, 2, 0.00)] + [TestCase(0.001234567890, 3, 0.001)] + [TestCase(0.0001234567890, 2, 0.00)] + [TestCase(0.0001234567890, 3, 0.000)] + public void ConvertFrom_FromDoubleToDouble_ReturnRoundedDouble( + double input, int numberOfPlaces, double expectedOutput) + { + // Setup + var converter = new RoundDoubleToDecimalPlacesConverter(numberOfPlaces); + + // Call + double result = (double)converter.ConvertFrom(input); + + // Assert + Assert.AreEqual(expectedOutput, result); + } + + [Test] + [TestCase(1.0, 2, 1.00)] + [TestCase(123456789.0, 3, 123456789.000)] + [TestCase(12345678.90, 2, 12345678.90)] + [TestCase(12345678.90, 3, 12345678.900)] + [TestCase(1234567.890, 2, 1234567.89)] + [TestCase(1234567.890, 3, 1234567.890)] + [TestCase(123456.7890, 2, 123456.79)] + [TestCase(123456.7890, 3, 123456.789)] + [TestCase(12345.67890, 2, 12345.68)] + [TestCase(12345.67890, 3, 12345.679)] + [TestCase(1234.567890, 2, 1234.57)] + [TestCase(1234.567890, 3, 1234.568)] + [TestCase(123.4567890, 2, 123.46)] + [TestCase(123.4567890, 3, 123.457)] + [TestCase(12.34567890, 2, 12.35)] + [TestCase(12.34567890, 3, 12.346)] + [TestCase(1.234567890, 2, 1.23)] + [TestCase(1.234567890, 3, 1.235)] + [TestCase(0.1234567890, 2, 0.12)] + [TestCase(0.1234567890, 3, 0.123)] + [TestCase(0.01234567890, 2, 0.01)] + [TestCase(0.01234567890, 3, 0.012)] + [TestCase(0.001234567890, 2, 0.00)] + [TestCase(0.001234567890, 3, 0.001)] + [TestCase(0.0001234567890, 2, 0.00)] + [TestCase(0.0001234567890, 3, 0.000)] + public void ConvertFrom_FromStringToDouble_ReturnRoundedDouble( + double input, int numberOfPlaces, double expectedOutput) + { + // Setup + var converter = new RoundDoubleToDecimalPlacesConverter(numberOfPlaces); + + CultureInfo currentCulture = CultureInfo.CurrentCulture; + string stringInput = input.ToString(currentCulture); + + // Call + double result = (double)converter.ConvertFrom(null, currentCulture, stringInput); + + // Assert + Assert.AreEqual(expectedOutput, result); + } } } \ No newline at end of file