forked from BeyondDimension/SteamTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberToInt32Format.cs
More file actions
52 lines (47 loc) · 1.72 KB
/
NumberToInt32Format.cs
File metadata and controls
52 lines (47 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace System
{
/// <summary>
/// 小数数字转换为整数数字格式
/// </summary>
public enum NumberToInt32Format
{
/// <summary>
/// 四舍五入
/// </summary>
RoundAwayFromZero,
/// <summary>
/// 五舍六入
/// </summary>
Round,
/// <summary>
/// 仅取整数
/// </summary>
Floor,
/// <summary>
/// 进一法
/// </summary>
Ceiling,
}
/// <summary>
/// Enum 扩展 <see cref="NumberToInt32Format"/>
/// </summary>
public static partial class NumberToInt32FormatEnumExtensions
{
public static int ToInt32(this float value, NumberToInt32Format format = NumberToInt32Format.Floor) => format switch
{
NumberToInt32Format.RoundAwayFromZero => (int)MathF.Round(value, MidpointRounding.AwayFromZero),
NumberToInt32Format.Round => (int)MathF.Round(value),
NumberToInt32Format.Floor => (int)MathF.Floor(value),
NumberToInt32Format.Ceiling => (int)MathF.Ceiling(value),
_ => throw new ArgumentOutOfRangeException(nameof(format), format, null),
};
public static int ToInt32(this double value, NumberToInt32Format format = NumberToInt32Format.Floor) => format switch
{
NumberToInt32Format.RoundAwayFromZero => (int)Math.Round(value, MidpointRounding.AwayFromZero),
NumberToInt32Format.Round => (int)Math.Round(value),
NumberToInt32Format.Floor => (int)Math.Floor(value),
NumberToInt32Format.Ceiling => (int)Math.Ceiling(value),
_ => throw new ArgumentOutOfRangeException(nameof(format), format, null),
};
}
}