Unity3D--大额数值单位转换

2 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Numdispose("45555555207"));
            string[] symbol = {"个", "千", "百万", "十亿", "万亿" };
            Console.WriteLine(NumUnit(45555555207, symbol));
            Console.WriteLine(FormatNum(45555555207, 3));
            Console.ReadKey();
        }

        /// <summary>
        /// 数字换算
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static string Numdispose(string num)
        {
            string[] symbol = { "K", "M", "B", "T", "aa", "ab", "ac", "ad" };

            string str1 = string.Empty;

            string str2 = string.Empty;

            if (num.Length > 6)
            {
                int a = (num.Length - 6) / 3;

                str1 = num.Substring(0, (num.Length - (3 * (a + 1))));

                int b = num.Length - (3 * (a + 1));

                str2 = num[b].ToString();

                if (int.Parse(str2) >= 5) str1 = (int.Parse(str1) + 1).ToString();

                if (str1.Length > 3) return str1.Insert(str1.Length - 3, ",") + symbol[a];

                return str1 + symbol[a];
            }
            if (num.Length > 3 && num.Length < 7)
            {
                return num.Insert(num.Length - 3, ",");
            }
            return num;
        }


        public static string NumUnit(double num, string[] unitArray)
        {
            double tempNum = num;
            if (tempNum < 10000)
            {
                return num.ToString("0");
            }

            int unitIndex = 0;
            while (tempNum / 10000 / 100 >= 1)
            {
                unitIndex++;
                if (unitIndex >= unitArray.Length)
                {
                    unitIndex = unitArray.Length - 1;
                    break;
                }
                tempNum /= 100;
            }
            return (tempNum / 10000).ToString("0.00") + unitArray[unitIndex];
        }

        /// <summary>
        /// 数值转换 效果最好
        /// </summary>
        /// <param name="coin">数值</param>
        /// <param name="fixNum">保留小数点数</param>
        /// <returns></returns>
        public static string FormatNum(decimal coin, int fixNum)
        {
            string textTemp = string.Empty;
            string text3 = "f" + fixNum.ToString();
            if (coin >= 100000000000000000000000000m)
            {
                textTemp = (coin / 100000000000000000000000000m).ToString(text3) + "ae";
            }
            else if (coin >= 1000000000000000000000000m)
            {
                textTemp = (coin / 1000000000000000000000000m).ToString(text3) + "ad";
            }
            else if (coin >= 1000000000000000000000m)
            {
                textTemp = (coin / 1000000000000000000000m).ToString(text3) + "ac";
            }
            else if (coin >= new decimal(1000000000000000000L))
            {
                textTemp = (coin / new decimal(1000000000000000000L)).ToString(text3) + "ab";
            }
            else if (coin >= new decimal(1000000000000000L))
            {
                textTemp = (coin / new decimal(1000000000000000L)).ToString(text3) + "aa";
            }
            else if (coin >= new decimal(1000000000000L))
            {
                textTemp = (coin / new decimal(1000000000000L)).ToString(text3) + "t";
            }
            else if (coin >= 1000000000m)
            {
                textTemp = (coin / 1000000000m).ToString(text3) + "b";
            }
            else if (coin >= 1000000m)
            {
                textTemp = (coin / 1000000m).ToString(text3) + "m";
            }
            else if (coin >= 1000m)
            {
                textTemp = (coin / 1000m).ToString(text3) + "k";
            }
            else
            {
                textTemp = coin.ToString("0");
            }

            return textTemp;
        }
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值