In .net, how do I choose between a Decimal and a Double(在 .net 中,如何在十进制和双精度之间进行选择)
问题描述
我们前几天在工作中讨论过这个问题,我希望有一个 Stackoverflow 问题我会向人们指出,所以就这样吧.)
We were discussing this the other day at work and I wish there was a Stackoverflow question I would point people at so here goes.)
有什么区别和一个十进制?- 您应该在什么时候(在什么情况下)始终使用
Double? - 什么时候(在什么情况下)您应该总是使用
十进制? - 在不属于上述两个阵营之一的情况下,需要考虑的驱动因素是什么?
- What is the difference between a
Doubleand aDecimal? - When (in what cases) should you always use a
Double? - When (in what cases) should you always use a
Decimal? - What’s the driving factors to consider in cases that don’t fall into one of the two camps above?
有很多问题与这个问题重叠,但它们往往是在询问某人在特定情况下应该做什么,而不是在一般情况下如何决定.
There are a lot of questions that overlap this question, but they tend to be asking what someone should do in a given case, not how to decide in the general case.
推荐答案
我通常会考虑 自然 vs 人造数量.
I usually think about natural vs artificial quantities.
自然量是指体重、身高和时间.这些永远不会被绝对准确地测量,并且很少有绝对精确的算术概念:您通常不应该将高度相加,然后确保结果完全符合预期.对这种数量使用 double.双打的范围很大,但精度有限;它们也非常快.
Natural quantities are things like weight, height and time. These will never be measured absolutely accurately, and there's rarely any idea of absolutely exact arithmetic on it: you shouldn't generally be adding up heights and then making sure that the result is exactly as expected. Use double for this sort of quantity. Doubles have a huge range, but limited precision; they're also extremely fast.
占主导地位的人造数量是金钱.有正好 10.52 美元"这样的东西,如果你加上 48 美分,你预计正好有 11 美元.对这种数量使用 decimal.理由:考虑到一开始是人为的,所涉及的数字也是人为的,旨在满足人类的需求——这意味着它们自然地以 10 为基数表示.使存储表示与人类表示相匹配.decimal 没有 double 的范围,但大多数人工量也不需要那个额外的范围.它也比 double 慢,但我个人有一个银行账户,它给我的正确答案比错误答案快:)
The dominant artificial quantity is money. There is such a thing as "exactly $10.52", and if you add 48 cents to it you expect to have exactly $11. Use decimal for this sort of quantity. Justification: given that it's artificial to start with, the numbers involved are artificial too, designed to meet human needs - which means they're naturally expressed in base 10. Make the storage representation match the human representation. decimal doesn't have the range of double, but most artificial quantities don't need that extra range either. It's also slower than double, but I'd personally have a bank account which gave me the right answer slowly than a wrong answer quickly :)
有关更多信息,我有关于 .NET 二进制浮点类型的文章 和 .NET 十进制类型.(请注意,decimal 也是浮点类型 - 但有问题的点"是小数点,而不是二进制点.)
For a bit more information, I have articles on .NET binary floating point types and the .NET decimal type. (Note that decimal is a floating point type too - but the "point" in question is a decimal point, not a binary point.)
这篇关于在 .net 中,如何在十进制和双精度之间进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 .net 中,如何在十进制和双精度之间进行选择
基础教程推荐
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- WPF 模态进度窗口 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
