繁体   English   中英

c#创建自定义“双”类型

[英]c# creating a custom “double” type

在我的应用程序中,我希望所有存储金额的属性都舍入到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
}

我建议如下:

  1. 创建一个名为MoneyAmount的新结构。
  2. 它包含一个字段: double
  3. 具有一个double参数的构造函数,此构造函数对值进行舍入并将其分配给内部字段。
  4. 将您可能需要的成员/运算符添加到结构中,使其具有与double相同的操作,如+, - 等。还可以从/向其他类型转换/转换。 每个操作都会生成一个带有舍入值的MoneyAmount新实例。
  5. 还要考虑实现IFormattableIComparableIConvertible接口。

简短的例子:

public struct MoneyAmount
{
    const int N = 4;
    private readonly double _value;

    public MoneyAmount(double value)
    {
        _value = Math.Round(value, N);
    }

    // Example of one member of double:
    public static MoneyAmount operator *(MoneyAmount d1, MoneyAmount d2) 
    {
        return new MoneyAmount(d1._value * d2._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>
    /// Explicit conversion from MoneyAmount to double. 
    /// Explicit: A cast operator is required.
    /// </summary>
    public static explicit operator double(MoneyAmount value)
    {
        return value._value;
    }

    /// <summary>
    /// Explicit conversion from MoneyAmount to int. 
    /// Explicit: A cast operator is required.
    /// </summary>
    public static explicit operator MoneyAmount(int value)
    {
        return new MoneyAmount(value);
    }

    /// <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;
    }

    // All other members here...
}

我意识到: double有很多成员......

使用这些运算符,可以使用以下代码:

MoneyAmount m = 1.50; // Assignment from a double.
MoneyAmount n = 10; // Assignment from an integer.
m += n; // Mathematical operation with another MoneyAmount .
m *= 10; // Mathematical operation with an integer.
m -= 12.50; // Mathematical operation with a double.

编辑

您可能想要实现的所有转换方法:

  • 显式MoneyAmount - > int
  • 显式MoneyAmount - > float
  • 显式MoneyAmount - > double
  • 显式MoneyAmount - >十进制

  • 隐式int - > MoneyAmount

  • 隐式浮点数 - > MoneyAmount
  • 隐式双 - > MoneyAmount
  • 隐式小数 - > MoneyAmount

您可能想要实现的所有数学运算:

  • MoneyAmount + MoneyAmount
  • MoneyAmount - MoneyAmount
  • MoneyAmount * MoneyAmount
  • MoneyAmount / MoneyAmount

您可能想要实现的所有关系操作:

  • MoneyAmount == MoneyAmount
  • MoneyAmount!= MoneyAmount
  • MoneyAmount> MoneyAmount
  • MoneyAmount> = MoneyAmount
  • MoneyAmount <MoneyAmount
  • MoneyAmount <= MoneyAmount

通过所有这些操作,您可以了解所有基础知识。

这很快变大了。 编写结构很容易,如@ MartinMulder的回答所示,但考虑到你需要重载多个运算符组合,以及包括一些隐式/显式转换。

数学和逻辑运算

考虑您可能想要在MoneyAmount上进行数学运算

  • MoneyAmount + MoneyAmount
  • MoneyAmount + double
  • MoneyAmount + int
  • MoneyAmount + decimal

这是+运算符的4次重载。 冲洗并重复-/* (可能为% )。 您还需要重载<<===>>= 这就像30个运算符重载。 唷! 这是很多静态方法。

public static MoneyAmount operator +(MoneyAmount d1, double d2) 
{
    return new MoneyAmount((decimal)(d1._value + d2));
}

显式/隐式演员

现在考虑代替这个代码

MoneyAmount m = new MoneyAmount(1.234);

你想这样做:

MoneyAmount m = 1.234;

这可以通过隐式转换运算符来实现。

public static implicit operator MoneyAmount(double d)
{
    return new MoneyAmount((decimal)d);
}

(你想要允许隐式转换的每种类型都需要一个)

另一个:

int i = 4;
MoneyAmount m = (MoneyAmount)i;

这是通过显式转换运算符重载完成的。

public static explicit operator MoneyAmount(double d)
{
    return new MoneyAmount((decimal)d);
}

(同样,对于您希望允许显式转换的每种类型都为1)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM