change
This commit is contained in:
36
Assets/Scripts/Utils/GameUtil.cs
Normal file
36
Assets/Scripts/Utils/GameUtil.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public static class GameUtil
|
||||
{
|
||||
/// <summary>
|
||||
/// 格式化数字显示,大于等于10000显示N万,保留1位小数点,整数1万、2万不要小数点
|
||||
/// </summary>
|
||||
/// <param name="number">要格式化的数字</param>
|
||||
/// <returns>格式化后的字符串</returns>
|
||||
public static string FormatNumber(this int number)
|
||||
{
|
||||
if (number < 10000)
|
||||
{
|
||||
return number.ToString(); // 小于10000直接显示
|
||||
}
|
||||
|
||||
// 转换为万为单位
|
||||
double valueInTenThousand = number / 10000.0;
|
||||
|
||||
// 判断是否为整数万
|
||||
if (Math.Abs(valueInTenThousand - Math.Floor(valueInTenThousand)) < 0.00001) // 处理浮点数精度
|
||||
{
|
||||
// 整数万,不显示小数点
|
||||
return $"{(int)valueInTenThousand}万";
|
||||
}
|
||||
|
||||
// 非整数万,向下取整并保留1位小数
|
||||
double flooredValue = Math.Floor(valueInTenThousand * 10) / 10;
|
||||
|
||||
// 处理浮点数精度,确保只显示1位小数
|
||||
return $"{flooredValue:F1}万";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user