Files
BabyVideo/Assets/Scripts/Utils/GameUtil.cs
2026-02-12 22:15:15 +08:00

36 lines
1.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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}万";
}
}
}