tl;dr:我的 Cur
怎么了? (货币)结构?
tl;dr 2: 请先阅读问题的其余部分,然后再用 float
举例说明或 double
. :-)
我知道这个问题在整个互联网上已经出现过无数次,但我还没有看到令人信服的答案,所以我想我会再问一次。
我不明白为什么使用非十进制数据类型不利于处理金钱。 (这是指存储二进制数字而不是十进制数字的数据类型。)
是的,比较两个 double
是不明智的与 a == b
.但是你可以很容易地说 a - b <= EPSILON
或类似的东西。
这种方法有什么问题?
例如,我刚刚制作了一个 struct
我认为在 C# 中可以正确处理金钱,而无需使用任何基于小数的数据格式:
struct Cur
{
private const double EPS = 0.00005;
private double val;
Cur(double val) { this.val = Math.Round(val, 4); }
static Cur operator +(Cur a, Cur b) { return new Cur(a.val + b.val); }
static Cur operator -(Cur a, Cur b) { return new Cur(a.val - b.val); }
static Cur operator *(Cur a, double factor) { return new Cur(a.val * factor); }
static Cur operator *(double factor, Cur a) { return new Cur(a.val * factor); }
static Cur operator /(Cur a, double factor) { return new Cur(a.val / factor); }
static explicit operator double(Cur c) { return Math.Round(c.val, 4); }
static implicit operator Cur(double d) { return new Cur(d); }
static bool operator <(Cur a, Cur b) { return (a.val - b.val) < -EPS; }
static bool operator >(Cur a, Cur b) { return (a.val - b.val) > +EPS; }
static bool operator <=(Cur a, Cur b) { return (a.val - b.val) <= +EPS; }
static bool operator >=(Cur a, Cur b) { return (a.val - b.val) >= -EPS; }
static bool operator !=(Cur a, Cur b) { return Math.Abs(a.val - b.val) < EPS; }
static bool operator ==(Cur a, Cur b) { return Math.Abs(a.val - b.val) > EPS; }
bool Equals(Cur other) { return this == other; }
override int GetHashCode() { return ((double)this).GetHashCode(); }
override bool Equals(object o) { return o is Cur && this.Equals((Cur)o); }
override string ToString() { return this.val.ToString("C4"); }
}
(很抱歉,将名称 Currency
更改为 Cur
,变量名称不佳,省略了 public
,布局也不好;我试图将其全部显示在屏幕上,以便您可以阅读它没有滚动。):)
你可以像这样使用它:
Currency a = 2.50;
Console.WriteLine(a * 2);
当然,C# 有 decimal
数据类型,但这不是重点——问题是为什么上面的是危险的,而不是为什么我们不应该使用 decimal
.
那么有人会介意为我提供一个真实世界的反例 一个在 C# 中会失败的危险语句吗?我想不出。
谢谢!
注意:我不争论是否decimal
是个不错的选择。我在问为什么说基于二进制的系统是不合适的。
最佳答案
对于积累和减少资金而言, float 并不稳定。这是您的实际示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BadFloat
{
class Program
{
static void Main(string[] args)
{
Currency yourMoneyAccumulator = 0.0d;
int count = 200000;
double increment = 20000.01d; //1 cent
for (int i = 0; i < count; i++)
yourMoneyAccumulator += increment;
Console.WriteLine(yourMoneyAccumulator + " accumulated vs. " + increment * count + " expected");
}
}
struct Currency
{
private const double EPSILON = 0.00005;
public Currency(double value) { this.value = value; }
private double value;
public static Currency operator +(Currency a, Currency b) { return new Currency(a.value + b.value); }
public static Currency operator -(Currency a, Currency b) { return new Currency(a.value - b.value); }
public static Currency operator *(Currency a, double factor) { return new Currency(a.value * factor); }
public static Currency operator *(double factor, Currency a) { return new Currency(a.value * factor); }
public static Currency operator /(Currency a, double factor) { return new Currency(a.value / factor); }
public static Currency operator /(double factor, Currency a) { return new Currency(a.value / factor); }
public static explicit operator double(Currency c) { return System.Math.Round(c.value, 4); }
public static implicit operator Currency(double d) { return new Currency(d); }
public static bool operator <(Currency a, Currency b) { return (a.value - b.value) < -EPSILON; }
public static bool operator >(Currency a, Currency b) { return (a.value - b.value) > +EPSILON; }
public static bool operator <=(Currency a, Currency b) { return (a.value - b.value) <= +EPSILON; }
public static bool operator >=(Currency a, Currency b) { return (a.value - b.value) >= -EPSILON; }
public static bool operator !=(Currency a, Currency b) { return Math.Abs(a.value - b.value) <= EPSILON; }
public static bool operator ==(Currency a, Currency b) { return Math.Abs(a.value - b.value) > EPSILON; }
public bool Equals(Currency other) { return this == other; }
public override int GetHashCode() { return ((double)this).GetHashCode(); }
public override bool Equals(object other) { return other is Currency && this.Equals((Currency)other); }
public override string ToString() { return this.value.ToString("C4"); }
}
}
在我的盒子上,这给出了 $4,000,002,000.0203 累计,而 C# 中预期为 4000002000。如果这在银行的许多交易中丢失,那将是一笔糟糕的交易 - 它不一定是大交易,只是很多。这有帮助吗?
关于c# - 为什么使用非十进制数据类型对金钱不利?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5768792/