Floating-point cheat sheet for 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
© Published at floating-point-gui.de under the Creative Commons Attribution License (BY)