title | description |
---|---|
Floating-point cheat sheet for C# |
Tips for using floating-point and decimal numbers in C# |
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
C# has a 128 bit limited-precision decimal type denoted by the keyword
decimal
:
decimal myMoney = 300.1m; // note m suffix on the literal
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
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