gpt4 book ai didi

c# 创建自定义 "double"类型

转载 作者:太空狗 更新时间:2023-10-30 00:13:58 25 4
gpt4 key购买 nike

在我的应用程序中,我希望所有存储金额的属性都四舍五入到 n 位小数。

为了代码清晰,我宁愿有一个自定义类型MoneyAmount,我的所有对应字段都会有,而不是必须在所有属性 getter/setter。

有没有一种巧妙的方法可以实现这一点?

我看到了this post关于重载赋值运算符 - 这是建议的方法吗?

编辑:考虑到多个 View ,我在此处发布了我导出的完整代码:

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. 同时考虑实现接口(interface) 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 --> 双
  • 显式 MoneyAmount --> 十进制

  • 隐式 int--> MoneyAmount

  • 隐式 float --> MoneyAmount
  • 隐式双倍--> MoneyAmount
  • 隐式小数 --> MoneyAmount

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

  • 金钱金额 + 金钱金额
  • MoneyAmount - MoneyAmount
  • 金钱金额 * 金钱金额
  • 金钱金额/金钱金额

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

  • 金钱金额 == 金钱金额
  • MoneyAmount != MoneyAmount
  • MoneyAmount > MoneyAmount
  • MoneyAmount >= MoneyAmount
  • 金钱金额<金钱金额
  • 金钱金额 <= 金钱金额

通过所有这些操作,您已经涵盖了所有基础知识。

关于c# 创建自定义 "double"类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38198739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com