少年修仙传客户端代码仓库
hch
2025-01-02 a0cc772e3de31fc11962a5ceec29fc58554fca09
Utility/UIHelper.cs
@@ -21,6 +21,49 @@
    public static readonly string no_breaking_space = "\u00A0"; //Text文本框输入空格自动换行问题 替换编码
    //不需要提取翻译的文本
    private static readonly string[] numbers = new string[] { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
    private static readonly string[] units = new string[] { "十", "百", "千", "万" };
    //数字转中文(只做小数字),海外版本调用改函数切换版本请直接返回数字
    public static string ChineseNumber(int number)
    {
        if (number == 0)
            return numbers[0];
        string result = "";
        int unitPlace = 1; // 位数,1 表示十位,2 表示百位,3 表示千位,4 表示万位
        bool needZero = false; // 是否需要添加 '零'
        while (number > 0)
        {
            int part = number % 10;
            if (part != 0)
            {
                if (needZero)
                    result = StringUtility.Contact(numbers[0] + result);
                result = numbers[part] + (unitPlace == 1 ? "" : units[unitPlace - 2]) + result; // 单位只在十、百、千、万内部使用
                needZero = false;
            }
            else
            {
                needZero = true;
            }
            number /= 10;
            unitPlace++;
        }
        if (result.StartsWith("一十"))
        {
            result = result.Substring(1);
        }
        return result;
    }
    #region UI通用
    public static void SetIconWithMoneyType(this Image _image, int moneyType)
    {
@@ -342,6 +385,31 @@
        }
    }
    // 转化大数值带单位的显示,如果小于200000则显示原值,否则显示大数值
    public static string ReplaceLargeNumEx(double num)
    {
        if (num < 200000)
        {
            //根据界面的显示假定值 200000,可根据实际情况调整
            return GetNumFormat(num);
        }
        else
        {
            return ReplaceLargeNum(num);
        }
    }
    //显示最多两位小数,并按地区显示格式
    // 如中文用F2 显示 1111.34,英文用N2 显示 1,111.34(带逗号)
    public static string GetNumFormat(double num)
    {
        if (num == (int)num)
        {
            return num.ToString("F0");
        }
        return num.ToString("F2").TrimEnd('0').TrimEnd('.');
    }
    public static float ReplacePercentage(float num, int isPer, int decimals = 1)
    {