Skip to content

Latest commit

 

History

History
75 lines (49 loc) · 2.44 KB

File metadata and controls

75 lines (49 loc) · 2.44 KB
title description
Floating-point cheat sheet for C#
Tips for using floating-point and decimal numbers in C#

Floating-Point Types

C# has IEEE 754 single and double precision types supported by keywords:

	float f = 0.1f; // 32 bit float, note f suffix
	double d = 0.1d; // 64 bit float, suffix optional

Decimal Types

C# has a 128 bit limited-precision decimal type denoted by the keyword decimal:

	decimal myMoney = 300.1m; // note m suffix on the literal

How to Round

The Math.Round() method works with the double and decimal types, and allows you to specify a rounding mode:

	Math.Round(1.25m, 1, MidpointRounding.AwayFromZero); // returns 1.3

Resources


title: 为 C# 准备的浮点小抄 description: Tips for using floating-point and decimal numbers in C#

Floating-Point Types

C# 用关键字来支持 IEEE 754 的单精度和双精度类型:

	float f = 0.1f; // 32 bit float, note f suffix
	double d = 0.1d; // 64 bit float, suffix optional

小数类型

C# 用关键字 decimal 来支持 128 位 limited-precision 小数类型:

	decimal myMoney = 300.1m; // note m suffix on the literal

如何取整

Math.Round() 方法与双精度和小数类型一直工作,现时也允许使用特定的 取整模式

	Math.Round(1.25m, 1, MidpointRounding.AwayFromZero); // returns 1.3

资源