diff --git a/src/Umbraco.Core/DecimalExtensions.cs b/src/Umbraco.Core/DecimalExtensions.cs new file mode 100644 index 0000000000..b4d74fdb2e --- /dev/null +++ b/src/Umbraco.Core/DecimalExtensions.cs @@ -0,0 +1,23 @@ +namespace Umbraco.Core +{ + /// + /// Provides extension methods for System.Decimal. + /// + /// See System.Decimal on MSDN and also + /// http://stackoverflow.com/questions/4298719/parse-decimal-and-filter-extra-0-on-the-right/4298787#4298787. + /// + public static class DecimalExtensions + { + /// + /// Gets the normalized value. + /// + /// The value to normalize. + /// The normalized value. + /// Normalizing changes the scaling factor and removes trailing zeroes, + /// so 1.2500m comes out as 1.25m. + public static decimal Normalize(this decimal value) + { + return value / 1.000000000000000000000000000000000m; + } + } +} diff --git a/src/Umbraco.Core/Models/Property.cs b/src/Umbraco.Core/Models/Property.cs index 89f9307ff7..49f336226b 100644 --- a/src/Umbraco.Core/Models/Property.cs +++ b/src/Umbraco.Core/Models/Property.cs @@ -174,7 +174,9 @@ namespace Umbraco.Core.Models case DataTypeDatabaseType.Decimal: var convDecimal = value.TryConvertTo(); if (convDecimal == false) ThrowTypeException(value, typeof(decimal), _propertyType.Alias); - value = convDecimal.Result; + // need to normalize the value (change the scaling factor and remove trailing zeroes) + // because the underlying database is going to mess with the scaling factor anyways. + value = convDecimal.Result.Normalize(); break; case DataTypeDatabaseType.Date: var convDateTime = value.TryConvertTo(); diff --git a/src/Umbraco.Core/Models/Rdbms/PropertyDataDto.cs b/src/Umbraco.Core/Models/Rdbms/PropertyDataDto.cs index 63e3104d12..380eaa3034 100644 --- a/src/Umbraco.Core/Models/Rdbms/PropertyDataDto.cs +++ b/src/Umbraco.Core/Models/Rdbms/PropertyDataDto.cs @@ -33,9 +33,23 @@ namespace Umbraco.Core.Models.Rdbms [NullSetting(NullSetting = NullSettings.Null)] public int? Integer { get; set; } + private decimal? _decimalValue; + [Column("dataDecimal")] [NullSetting(NullSetting = NullSettings.Null)] - public decimal? Decimal { get; set; } + public decimal? Decimal + { + get + { + return _decimalValue; + } + set + { + // need to normalize the value (change the scaling factor and remove trailing zeroes) + // because the underlying database probably has messed with the scaling factor. + _decimalValue = value.HasValue ? (decimal?) value.Value.Normalize() : null; + } + } [Column("dataDate")] [NullSetting(NullSetting = NullSettings.Null)] diff --git a/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs b/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs index a69539e9ea..446bd426ad 100644 --- a/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs +++ b/src/Umbraco.Core/Persistence/Factories/PropertyFactory.cs @@ -99,7 +99,7 @@ namespace Umbraco.Core.Persistence.Factories decimal val; if (decimal.TryParse(property.Value.ToString(), out val)) { - dto.Decimal = val; + dto.Decimal = val; // property value should be normalized already } } else if (property.DataTypeDatabaseType == DataTypeDatabaseType.Date && property.Value != null && string.IsNullOrWhiteSpace(property.Value.ToString()) == false) diff --git a/src/Umbraco.Core/Umbraco.Core.csproj b/src/Umbraco.Core/Umbraco.Core.csproj index 3e14fc7758..e303ef4993 100644 --- a/src/Umbraco.Core/Umbraco.Core.csproj +++ b/src/Umbraco.Core/Umbraco.Core.csproj @@ -305,6 +305,7 @@ +