惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Tor Project blog
Cloudbric
Cloudbric
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
N
News | PayPal Newsroom
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 三生石上(FineUI控件)
E
Exploit-DB.com RSS Feed
WordPress大学
WordPress大学
F
Fortinet All Blogs
O
OpenAI News
IT之家
IT之家
Vercel News
Vercel News
G
Google Developers Blog
Spread Privacy
Spread Privacy
T
The Blog of Author Tim Ferriss
T
The Exploit Database - CXSecurity.com
V
V2EX - 技术
I
Intezer
N
News and Events Feed by Topic
W
WeLiveSecurity
宝玉的分享
宝玉的分享
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
I
InfoQ
The GitHub Blog
The GitHub Blog
C
Check Point Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LangChain Blog
月光博客
月光博客
Microsoft Security Blog
Microsoft Security Blog
C
CERT Recently Published Vulnerability Notes
Hugging Face - Blog
Hugging Face - Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Comments on: Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Announcements
Recent Announcements
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
Cyberwarzone
Cyberwarzone
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - PanPan003

python environment settings xUnit Theory: Working With InlineData, MemberData, ClassData docker network - container networking KEDA — Kubernetes Based Event Driven Auto scaling(转载) rabbit MQ —— ha-sync-mode. message 同步/ 丢失 in new pods rabbit MQ —— ha-mode, message 同步策列:所有nodes or one nodes 博文阅读密码验证 - 博客园 Kubernetes hpa container scale up/ down 原理 in kubernetes 博文阅读密码验证 - 博客园 Windows证书管理器 && SSL certification && WSL-Docker: curl: (60) SSL certificate problem: unable to get local issuer certificate 博文阅读密码验证 - 博客园 httpclient in .net _ 压缩 MongoDB 大文件处理 _ Building MongoDB Applications with Binary Files Using GridFS 博文阅读密码验证 - 博客园 Mock —— .Protected() .Setup XUnit —— Record.Exception —— Stop Using Assert.Throws in Your BDD Unit Tests RabbitMQ _ How to Close a Channel What is .NET MAUI? —— a cross-platform framework for creating native mobile and desktop apps with C# and XAML.
decimal, double在 c#中的区别
PanPan003 · 2026-03-09 · via 博客园 - PanPan003

一句话总结

  • double二进制浮点(IEEE 754,64 位),速度快、范围大,但对十进制小数(比如货币)有不可避免的精度误差。适合科学计算、图形、向量、概率、机器学习、物理仿真等。
  • decimal十进制浮点(128 位,28–29 位有效十进制数),对十进制小数表示更精确,但范围小、速度慢。适合金融/会计、价格、税率、金额等。

核心差异速览

方面doubledecimal
位宽 64 位 128 位
表示 二进制浮点(base-2) 十进制浮点(base-10,96 位整数 + 0–28 的尺度因子)
有效数字 ~15–17 位十进制有效数字 28–29 位十进制有效数字
数值范围 ~ ±1.7E308 ~ ±7.9E28
性能 更快(硬件指令支持更强) 更慢(软件实现为主)
典型用途 科学计算、几何、物理、图形 金融、价格、税务、货币累计
字面量后缀 默认浮点字面量是 double(如 0.1 必须加 m/M 后缀(如 0.1m
比较策略 浮点误差需容忍区间(epsilon) 一般能做精确相等(十进制场景)

注意:“更精确”仅指十进制小数(例如 0.1、0.01 这些十进制能有限表示的数)。在“科学计算精度”意义上,double相对误差控制更适合数值分析,但对金融小数累加不友好。

为什么会有误差?

  • double二进制 表示,像 0.1 在二进制是一个无限循环小数,只能近似存储 → 计算会出现微小误差。
  • decimal十进制 缩放存储,0.1 可以被精确表示,所以 0.1 + 0.2 在 decimal 下能得到准确的 0.3。

典型示例对比

1) 金额计算(推荐 decimal

using System;

class Program
{
    static void Main()
    {
        // double:二进制浮点误差
        double d1 = 0.1;
        double d2 = 0.2;
        double dSum = d1 + d2;
        Console.WriteLine($"double: 0.1 + 0.2 = {dSum:R}");   // 可能是 0.30000000000000004

        // decimal:十进制精确表示
        decimal m1 = 0.1m;
        decimal m2 = 0.2m;
        decimal mSum = m1 + m2;
        Console.WriteLine($"decimal: 0.1m + 0.2m = {mSum}");  // 0.3
    }
}

输出(示例):

double: 0.1 + 0.2 = 0.30000000000000004
decimal: 0.1m + 0.2m = 0.3

2) 比较策略(double 用容忍区间,decimal 直接等于)

posted @ 2026-03-09 11:59  PanPan003  阅读(38)  评论()    收藏  举报