In my application I want all my properties storing money amounts to be rounded to n
decimal places.
For code clarity, I'd rather have a custom type MoneyAmount
which all my corresponding fields would have, instead of having to put a `Math.Round(value, n)' in all the property getters/setters.
Is there a neat way to achieve this?
I saw this post about overloading assignment operators - is this the suggested approach?
EDIT: Given the multiple views, I post the full code I derived here:
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
}