浅谈Unity之阿拉伯数字转中文汉字

简介: 阿拉伯数字转中文汉字

前言:每日记录自己学习unity的心得和体会,小弟才疏学浅,如有错误的地方,欢迎大佬们指正,感谢~


直接调用即可

代码如下:

using System;

using System.Collections;

using System.Collections.Generic;

using System.Text;

using UnityEngine;

public class ToolShu  

{

   /// <summary>

   /// 阿拉伯数字转换中文汉字  数字大写

   /// </summary>

   public static string Rell(int input)  

   {

       string ret = null;

       int input2 = Math.Abs(input);

       string resource = "零一二三四五六七八九";

       string unit = "个十百千万亿兆京垓秭穰沟涧正载极";

       if (input > Math.Pow(10, 4 * (unit.Length - unit.IndexOf('万'))))

       {

           throw new Exception("the input is too big,input:" + input);

       }

       Func<int, List<List<int>>> splitNumFunc = (val) => {

           int i = 0;

           int mod;

           int val_ = val;

           List<List<int>> splits = new List<List<int>>();

           List<int> splitNums;

           do

           {

               mod = val_ % 10;

               val_ /= 10;

               if (i % 4 == 0)

               {

                   splitNums = new List<int>();

                   splitNums.Add(mod);

                   if (splits.Count == 0)

                   {

                       splits.Add(splitNums);

                   }

                   else

                   {

                       splits.Insert(0, splitNums);

                   }

               }

               else

               {

                   splitNums = splits[0];

                   splitNums.Insert(0, mod);

               }

               i++;

           } while (val_ > 0);

           return splits;

       };

       Func<List<List<int>>, string> hommizationFunc = (data) => {

           List<StringBuilder> builders = new List<StringBuilder>();

           for (int i = 0; i < data.Count; i++)

           {

               List<int> data2 = data[i];

               StringBuilder newVal = new StringBuilder();

               for (int j = 0; j < data2.Count;)

               {

                   if (data2[j] == 0)

                   {

                       int k = j + 1;

                       for (; k < data2.Count && data2[k] == 0; k++) ;

                       //个位不是0,前面补一个零

                       newVal.Append('零');

                       j = k;

                   }

                   else

                   {

                       newVal.Append(resource[data2[j]]).Append(unit[data2.Count - 1 - j]);

                       j++;

                   }

               }

               if (newVal[newVal.Length - 1] == '零' && newVal.Length > 1)

               {

                   newVal.Remove(newVal.Length - 1, 1);

               }

               else if (newVal[newVal.Length - 1] == '个')

               {

                   newVal.Remove(newVal.Length - 1, 1);

               }

               if (i == 0 && newVal.Length > 1 && newVal[0] == '一' && newVal[1] == '十')

               {//一十 --> 十

                   newVal.Remove(0, 1);

               }

               builders.Add(newVal);

           }

           StringBuilder sb = new StringBuilder();

           for (int i = 0; i < builders.Count; i++)

           {//拼接

               if (builders.Count == 1)

               {//个位数

                   sb.Append(builders[i]);

               }

               else

               {

                   if (i == builders.Count - 1)

                   {//万位以下的

                       if (builders[i][builders[i].Length - 1] != '零')

                       {//十位以上的不拼接"零"

                           sb.Append(builders[i]);

                       }

                   }

                   else

                   {//万位以上的

                       if (builders[i][0] != '零')

                       {//零万零亿之类的不拼接

                           sb.Append(builders[i]).Append(unit[unit.IndexOf('千') + builders.Count - 1 - i]);

                       }

                   }

               }

           }

           return sb.ToString();

       };

       List<List<int>> ret_split = splitNumFunc(input2);

       ret = hommizationFunc(ret_split);

       if (input < 0) ret = "-" + ret;

       return ret;

   }

   /// <summary>

   /// 转成钱好像也是可以的 阿拉伯数字转成中文大写

   /// </summary>

   /// <param name="input"></param>

   /// <returns></returns>

   public static string Rel(int input)

   {

       string ret = null;

       int input2 = Math.Abs(input);

       string resource = "零壹贰叁肆伍陆柒捌玖";

       string unit = "个十百千万亿兆京垓秭穰沟涧正载极";

       if (input > Math.Pow(10, 4 * (unit.Length - unit.IndexOf('万'))))

       {

           throw new Exception("the input is too big,input:" + input);

       }

       Func<int, List<List<int>>> splitNumFunc = (val) => {

           int i = 0;

           int mod;

           int val_ = val;

           List<List<int>> splits = new List<List<int>>();

           List<int> splitNums;

           do

           {

               mod = val_ % 10;

               val_ /= 10;

               if (i % 4 == 0)

               {

                   splitNums = new List<int>();

                   splitNums.Add(mod);

                   if (splits.Count == 0)

                   {

                       splits.Add(splitNums);

                   }

                   else

                   {

                       splits.Insert(0, splitNums);

                   }

               }

               else

               {

                   splitNums = splits[0];

                   splitNums.Insert(0, mod);

               }

               i++;

           } while (val_ > 0);

           return splits;

       };

       Func<List<List<int>>, string> hommizationFunc = (data) => {

           List<StringBuilder> builders = new List<StringBuilder>();

           for (int i = 0; i < data.Count; i++)

           {

               List<int> data2 = data[i];

               StringBuilder newVal = new StringBuilder();

               for (int j = 0; j < data2.Count;)

               {

                   if (data2[j] == 0)

                   {

                       int k = j + 1;

                       for (; k < data2.Count && data2[k] == 0; k++) ;

                       //个位不是0,前面补一个零

                       newVal.Append('零');

                       j = k;

                   }

                   else

                   {

                       newVal.Append(resource[data2[j]]).Append(unit[data2.Count - 1 - j]);

                       j++;

                   }

               }

               if (newVal[newVal.Length - 1] == '零' && newVal.Length > 1)

               {

                   newVal.Remove(newVal.Length - 1, 1);

               }

               else if (newVal[newVal.Length - 1] == '个')

               {

                   newVal.Remove(newVal.Length - 1, 1);

               }

               if (i == 0 && newVal.Length > 1 && newVal[0] == '一' && newVal[1] == '十')

               {//一十 --> 十

                   newVal.Remove(0, 1);

               }

               builders.Add(newVal);

           }

           StringBuilder sb = new StringBuilder();

           for (int i = 0; i < builders.Count; i++)

           {//拼接

               if (builders.Count == 1)

               {//个位数

                   sb.Append(builders[i]);

               }

               else

               {

                   if (i == builders.Count - 1)

                   {//万位以下的

                       if (builders[i][builders[i].Length - 1] != '零')

                       {//十位以上的不拼接"零"

                           sb.Append(builders[i]);

                       }

                   }

                   else

                   {//万位以上的

                       if (builders[i][0] != '零')

                       {//零万零亿之类的不拼接

                           sb.Append(builders[i]).Append(unit[unit.IndexOf('千') + builders.Count - 1 - i]);

                       }

                   }

               }

           }

           return sb.ToString();

       };

       List<List<int>> ret_split = splitNumFunc(input2);

       ret = hommizationFunc(ret_split);

       if (input < 0) ret = "-" + ret;

       return ret;

   }

}


具体用法:ToolShu.Rell(100);

相关文章
|
C# 图形学
unity使用BMFont制作位图字体
unity也能制作艺术字拉
unity使用BMFont制作位图字体
|
1月前
|
人工智能 JSON 网络协议
AI 大模型 LLM API 深入解析:Gemini 3.0 Pro 的 SSE 流式响应与大模型接口跨区域延迟优化实践
本文对比Google Vertex AI与OpenAI在SSE协议处理上的差异,针对跨洋网络高延迟问题,提出通过聚合层优化TTFT。结合GRPC与REST的适配挑战,引入协议转换网关,实测P99延迟降低75%,显著提升连接稳定性与首 token 速度。
198 2
|
API C# 图形学
Unity调用Windows弹出确认框
在 Unity 中调用 Windows 弹出确认框,可通过 Windows API 或 .NET 框架实现。使用 Windows API 的方式是通过 P/Invoke 技术调用 MessageBox 函数,创建模态对话框。代码示例展示了如何在应用退出时弹出确认框,用户选择“确定”则退出游戏。此方法也适用于 ALT+F4 触发的退出确认。
|
存储 API 图形学
Unity 给Animator动画添加事件(动态的)
在 Unity 中,通过动画事件系统可在动画播放的特定时间点触发自定义函数。动态添加事件的步骤包括获取 `AnimationClip` 对象,创建并添加 `AnimationEvent`,最后调用 `Rebind()` 更新动画控制器。示例代码展示了如何在动画开始、中间和结束时触发事件,实现与游戏逻辑的交互。
|
存储 Java Linux
【Linux专题_02】Linux安装JDK1.8
【Linux专题_02】Linux安装JDK1.8
521 1
|
API 开发者
币安合约现货策略交易接口API对接开发源代码详情
# 生成签名的函数(示例) def generate_signature(params, secret): signature = '' for key in sorted(params.keys()):
|
定位技术 图形学 开发者
【Unity实战】切换场景加载进度和如何在后台异步加载具有庞大世界的游戏场景,实现无缝衔接(附项目源码)
【Unity实战】切换场景加载进度和如何在后台异步加载具有庞大世界的游戏场景,实现无缝衔接(附项目源码)
2081 1
|
图形学
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
949 0
|
IDE JavaScript 编译器
《Solidity 简易速速上手小册》第2章:搭建 Solidity 开发环境(2024 最新版)
《Solidity 简易速速上手小册》第2章:搭建 Solidity 开发环境(2024 最新版)
3468 0

热门文章

最新文章