Meh*_*dad 8 c# double types currency
tl;博士:我的Cur(货币)结构出了什么问题?
tl;博士2: 在用floator 表示例子之前,请先阅读其余的问题double.:-)
我知道这个问题在互联网上出现过很多次,但我还没有看到令人信服的答案,所以我想我再问一次.
我不明白为什么使用非十进制数据类型不利于处理钱.(指的是存储二进制数字而不是十进制数字的数据类型.)
没错,比较两个doubles是不明智的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"); }
}
Run Code Online (Sandbox Code Playgroud)
(对不起,更改名称Currency到Cur,穷人的变量名,而省略了public,而对于不良的布局;我试图适应这一切在屏幕上,让你可以无需滚动阅读.):)
您可以像以下一样使用它:
Currency a = 2.50;
Console.WriteLine(a * 2);
Run Code Online (Sandbox Code Playgroud)
当然,C#具有decimal数据类型,但这不是重点 - 问题是为什么上述是危险的,而不是我们不应该使用的原因decimal.
那么有人会介意给我一个真实世界的反例,这个反言会在C#中失败吗?我什么都想不到.
谢谢!
注意:我不是在争论是否decimal是一个好的选择.我在问为什么基于二进制的系统被认为是不合适的.
J T*_*ana 10
浮动对于累积和减少资金是不稳定的.这是你的实际例子:
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"); }
}
}
Run Code Online (Sandbox Code Playgroud)
在我的盒子上,累计产生4,000,002,000.0203美元,而C#预期为4000002000.如果它在银行的许多交易中丢失了,这是一个糟糕的交易 - 它不一定是大的,只有很多.这有帮助吗?