在我的应用程序中,我希望所有存储货币金额的属性都能四舍五入到
为了代码清晰,我宁愿使用自定义类型
有没有一种简洁的方法来实现这个?
我看到了关于重载赋值运算符的这篇文章 - 这是建议的方法吗?
编辑: 考虑到多个视图,我在此发布了我得出的完整代码:
n
位小数。为了代码清晰,我宁愿使用自定义类型
MoneyAmount
,而不是在所有属性的getter/setter中放置`Math.Round(value, n)`。有没有一种简洁的方法来实现这个?
我看到了关于重载赋值运算符的这篇文章 - 这是建议的方法吗?
编辑: 考虑到多个视图,我在此发布了我得出的完整代码:
public struct MoneyAmount {
const int N = 4;
private readonly double _value;
public MoneyAmount(double value) {
_value = Math.Round(value, N);
}
#region mathematical operators
public static MoneyAmount operator +(MoneyAmount d1, MoneyAmount d2) {
return new MoneyAmount(d1._value + d2._value);
}
public static MoneyAmount operator -(MoneyAmount d1, MoneyAmount d2) {
return new MoneyAmount(d1._value - d2._value);
}
public static MoneyAmount operator *(MoneyAmount d1, MoneyAmount d2) {
return new MoneyAmount(d1._value * d2._value);
}
public static MoneyAmount operator /(MoneyAmount d1, MoneyAmount d2) {
return new MoneyAmount(d1._value / d2._value);
}
#endregion
#region logical operators
public static bool operator ==(MoneyAmount d1, MoneyAmount d2) {
return d1._value == d2._value;
}
public static bool operator !=(MoneyAmount d1, MoneyAmount d2) {
return d1._value != d2._value;
}
public static bool operator >(MoneyAmount d1, MoneyAmount d2) {
return d1._value > d2._value;
}
public static bool operator >=(MoneyAmount d1, MoneyAmount d2) {
return d1._value >= d2._value;
}
public static bool operator <(MoneyAmount d1, MoneyAmount d2) {
return d1._value < d2._value;
}
public static bool operator <=(MoneyAmount d1, MoneyAmount d2) {
return d1._value <= d2._value;
}
#endregion
#region Implicit conversions
/// <summary>
/// Implicit conversion from int to MoneyAmount.
/// Implicit: No cast operator is required.
/// </summary>
public static implicit operator MoneyAmount(int value) {
return new MoneyAmount(value);
}
/// <summary>
/// Implicit conversion from float to MoneyAmount.
/// Implicit: No cast operator is required.
/// </summary>
public static implicit operator MoneyAmount(float value) {
return new MoneyAmount(value);
}
/// <summary>
/// Implicit conversion from double to MoneyAmount.
/// Implicit: No cast operator is required.
/// </summary>
public static implicit operator MoneyAmount(double value) {
return new MoneyAmount(value);
}
/// <summary>
/// Implicit conversion from decimal to MoneyAmount.
/// Implicit: No cast operator is required.
/// </summary>
public static implicit operator MoneyAmount(decimal value) {
return new MoneyAmount(Convert.ToDouble(value));
}
#endregion
#region Explicit conversions
/// <summary>
/// Explicit conversion from MoneyAmount to int.
/// Explicit: A cast operator is required.
/// </summary>
public static explicit operator int(MoneyAmount value) {
return (int)value._value;
}
/// <summary>
/// Explicit conversion from MoneyAmount to float.
/// Explicit: A cast operator is required.
/// </summary>
public static explicit operator float(MoneyAmount value) {
return (float)value._value;
}
/// <summary>
/// Explicit conversion from MoneyAmount to double.
/// Explicit: A cast operator is required.
/// </summary>
public static explicit operator double(MoneyAmount value) {
return (double)value._value;
}
/// <summary>
/// Explicit conversion from MoneyAmount to decimal.
/// Explicit: A cast operator is required.
/// </summary>
public static explicit operator decimal(MoneyAmount value) {
return Convert.ToDecimal(value._value);
}
#endregion
}
decimal
类型更为适合。 - Dmitry BychenkoMoney
是一种模式。事实上,它甚至更加复杂,因为有某些规则处理最小化舍入误差(银行家舍入算法只是其中一种技术),以及分配丢失的分数,使得任何东西都不会丢失。其他规则用于管理货币转换,其他规则用于管理每种货币允许的小数位数和内部计算的小数位数。 - Panagiotis Kanavos