U4-8592 - fix decimal scaling factor issue

This commit is contained in:
Stephan
2016-06-28 10:09:11 +02:00
parent 09526e6db4
commit db79690501
5 changed files with 43 additions and 3 deletions

View File

@@ -0,0 +1,23 @@
namespace Umbraco.Core
{
/// <summary>
/// Provides extension methods for System.Decimal.
/// </summary>
/// <remarks>See System.Decimal on MSDN and also
/// http://stackoverflow.com/questions/4298719/parse-decimal-and-filter-extra-0-on-the-right/4298787#4298787.
/// </remarks>
public static class DecimalExtensions
{
/// <summary>
/// Gets the normalized value.
/// </summary>
/// <param name="value">The value to normalize.</param>
/// <returns>The normalized value.</returns>
/// <remarks>Normalizing changes the scaling factor and removes trailing zeroes,
/// so 1.2500m comes out as 1.25m.</remarks>
public static decimal Normalize(this decimal value)
{
return value / 1.000000000000000000000000000000000m;
}
}
}

View File

@@ -174,7 +174,9 @@ namespace Umbraco.Core.Models
case DataTypeDatabaseType.Decimal:
var convDecimal = value.TryConvertTo<decimal>();
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<DateTime>();

View File

@@ -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)]

View File

@@ -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)

View File

@@ -305,6 +305,7 @@
<Compile Include="DatabaseContext.cs" />
<Compile Include="DataTableExtensions.cs" />
<Compile Include="DateTimeExtensions.cs" />
<Compile Include="DecimalExtensions.cs" />
<Compile Include="DelegateExtensions.cs" />
<Compile Include="DictionaryExtensions.cs" />
<Compile Include="Dictionary\CultureDictionaryFactoryResolver.cs" />