浅谈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);

相关文章
|
存储 监控 Linux
在 CentOS 7 中如何进行磁盘分区和挂载的最佳实践。
【10月更文挑战第7天】本文详细介绍了在 CentOS 7 中如何进行磁盘分区和挂载的最佳实践。通过合理规划和管理磁盘空间,可以提高系统的稳定性和可维护性。具体步骤包括确认未使用的硬盘、创建分区、格式化分区、创建挂载点、挂载分区以及编辑 `/etc/fstab` 文件实现永久挂载。此外,还分享了备份数据、分区规划、权限设置和监控磁盘使用等最佳实践。这些技能对 IT 专业人员来说至关重要。
1015 3
|
C# 图形学
unity使用BMFont制作位图字体
unity也能制作艺术字拉
unity使用BMFont制作位图字体
|
JSON atlas 图形学
unity之spine骨骼动画使用
unity实现spine骨骼动画使用
unity之spine骨骼动画使用
|
3月前
|
JavaScript Linux API
OpenClaw(Clawdbot)避坑指南实测:本地(Win11/Mac/Linux)搭建 VS 阿里云轻量服务器部署
OpenClaw(前身为Clawdbot、Moltbot)作为2026年最火爆的开源AI代理工具,GitHub星标已飙升至250K,凭借“自然语言驱动自动化”的核心能力,可操控浏览器、读写本地文件、生成代码、对接办公软件,成为普通人解放双手的“AI全能助手”。但伴随热度而来的是部署门槛的困扰——无数新手反馈,跟着旧教程操作要么装不上,要么装上就崩,甚至因API配置错误、权限问题导致无法正常使用,尤其Windows本地安装与阿里云部署的差异的让很多人陷入选择困境。
2627 1
|
5月前
|
人工智能 JSON 网络协议
AI 大模型 LLM API 深入解析:Gemini 3.0 Pro 的 SSE 流式响应与大模型接口跨区域延迟优化实践
本文对比Google Vertex AI与OpenAI在SSE协议处理上的差异,针对跨洋网络高延迟问题,提出通过聚合层优化TTFT。结合GRPC与REST的适配挑战,引入协议转换网关,实测P99延迟降低75%,显著提升连接稳定性与首 token 速度。
497 2
|
人工智能 自然语言处理 Java
IDEA中使用DeepSeek满血版的手把手教程来了!
本文主要介绍阿里云推出的AI编码助手——通义灵码在代码编写、智能问答、bug修复等方面的功能。
IDEA中使用DeepSeek满血版的手把手教程来了!
|
存储 图形学 索引
unity 使物体跟随路径点自动移动位置
在Unity中,物体沿路径点自动移动的核心原理是通过预设路径点,控制物体依次移动。路径点可用空对象或三维向量数组定义,并按顺序存储。移动时,计算当前位置与下一个路径点的向量差以确定方向,使用`Vector3.MoveTowards`逐步靠近目标点。代码实现包括路径点设置、移动控制及插值计算,确保物体平滑移动和旋转。
|
API 开发者
币安合约现货策略交易接口API对接开发源代码详情
# 生成签名的函数(示例) def generate_signature(params, secret): signature = '' for key in sorted(params.keys()):
|
存储 JSON Ubuntu
如何使用 Lua 脚本进行更复杂的网络请求,比如 POST 请求?
如何使用 Lua 脚本进行更复杂的网络请求,比如 POST 请求?
|
图形学
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
【unity小技巧】unity读excel配置表操作,excel转txt文本,并读取txt文本内容,实例说明
1168 0

热门文章

最新文章