Archived
添加一个将数字转为字符的方法,比如 510会读成五百一十,9009会读成九千零九,理论上最大支持到九千九百九十九亿九千九百九十九万九千九百九十九,但不是极限(需要有一些格式设定才能突破极限)。
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using UnityEngine;
|
||||
@@ -37,5 +38,82 @@ namespace XericLibrary.Runtime.MacroLibrary
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 区域与语言
|
||||
|
||||
#if false
|
||||
/// <summary>
|
||||
/// 将一个数值转为中文小写格式
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
public static string NumberToChinese_Format(int number)
|
||||
{
|
||||
CultureInfo cultureInfo = new CultureInfo("zh-CN");
|
||||
|
||||
// 使用文化信息中的数字格式来将数字转换为中文
|
||||
return string.Format(cultureInfo, "{0:R}", number);
|
||||
// 遗憾的是会报R无效
|
||||
}
|
||||
#endif
|
||||
|
||||
private static readonly string[] chineseNumber = new string[]
|
||||
{"零","一","二","三","四","五","六","七","八","九"};
|
||||
private static readonly string[] chineseDigit = new string[]
|
||||
{ "","十","百","千","万","十","百","千","亿","十","百","千","亿"};
|
||||
|
||||
/// <summary>
|
||||
/// 将一个数值转为中文小写格式;
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
private static string NumberToChinese(int number, out bool needFillZero, int digit = 0)
|
||||
{
|
||||
needFillZero = false;
|
||||
|
||||
// 取消输出最高位的零
|
||||
if(number == 0)
|
||||
return "";
|
||||
|
||||
// 高位拼接一个当前位
|
||||
if(number > 0)
|
||||
{
|
||||
int digitNumber = number % 10;
|
||||
var result = NumberToChinese(number / 10, out var fillZero, digit + 1);
|
||||
|
||||
// 此处为零,后续可能需要填充一个零
|
||||
needFillZero = digitNumber == 0;
|
||||
|
||||
// 如果此处为零,则等待低位处理填充
|
||||
var proclitic =
|
||||
needFillZero ? "" : (fillZero ? chineseNumber[0] : "") + chineseNumber[digitNumber] + chineseDigit[digit] ;
|
||||
return result + proclitic;
|
||||
}
|
||||
|
||||
// 输出最高位
|
||||
return chineseNumber[number] + chineseDigit[digit];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将一个数值转为中文小写格式,并确保读写格式满足日常使用;
|
||||
/// 比如 510会读成五百一十,9009会读成九千零九,
|
||||
/// 最大支持到九千亿,理论上并不是它的极限,只要继续套格式即可。
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <returns></returns>
|
||||
public static string NumberToChinese(int number)
|
||||
{
|
||||
string guide = "";
|
||||
// 如果为零直接输出,因为单个0没法读
|
||||
if(number == 0)
|
||||
return chineseNumber[0];
|
||||
// 如果为负就在前面加上负
|
||||
if(number < 0)
|
||||
guide = "负";
|
||||
// 合并格式化
|
||||
return guide + NumberToChinese(Mathf.Abs(number), out var zero);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "com.lrss3.xericlibrary",
|
||||
"displayName": "Xeric Library",
|
||||
"version": "0.2.1",
|
||||
"version": "0.2.2",
|
||||
"unity": "2021.3",
|
||||
"description": "\u901a\u7528\u5e93\u63d2\u4ef6\u3002"
|
||||
}
|
||||
Reference in New Issue
Block a user