本帖最后由 BH39125 于 2026-5-23 01:39 编辑
using System.Text;
namespace 中文基础工具库;
public static class 数值
{
public static int 绝对值(int 值)
{
return Math.Abs(值);
}
public static double 绝对值(double 值)
{
return Math.Abs(值);
}
public static decimal 绝对值(decimal 值)
{
return Math.Abs(值);
}
public static int 最大值(int 值1, int 值2)
{
return Math.Max(值1, 值2);
}
public static double 最大值(double 值1, double 值2)
{
return Math.Max(值1, 值2);
}
public static int 最小值(int 值1, int 值2)
{
return Math.Min(值1, 值2);
}
public static double 最小值(double 值1, double 值2)
{
return Math.Min(值1, 值2);
}
public static int 取整(double 值)
{
return (int)Math.Floor(值);
}
public static int 四舍五入(double 值)
{
return (int)Math.Round(值);
}
public static double 向上取整(double 值)
{
return Math.Ceiling(值);
}
public static double 向下取整(double 值)
{
return Math.Floor(值);
}
public static double 平方(double 值)
{
return Math.Pow(值, 2);
}
public static double 幂(double 底数, double 指数)
{
return Math.Pow(底数, 指数);
}
public static double 平方根(double 值)
{
return Math.Sqrt(值);
}
public static double 对数(double 值)
{
return Math.Log(值);
}
public static double 常用对数(double 值)
{
return Math.Log10(值);
}
public static double 自然对数(double 值)
{
return Math.Log(值);
}
public static double 正弦(double 角度)
{
return Math.Sin(角度 * Math.PI / 180);
}
public static double 余弦(double 角度)
{
return Math.Cos(角度 * Math.PI / 180);
}
public static double 正切(double 角度)
{
return Math.Tan(角度 * Math.PI / 180);
}
public static double 反正弦(double 值)
{
return Math.Asin(值) * 180 / Math.PI;
}
public static double 反余弦(double 值)
{
return Math.Acos(值) * 180 / Math.PI;
}
public static double 反正切(double 值)
{
return Math.Atan(值) * 180 / Math.PI;
}
public static bool 是偶数(int 值)
{
return 值 % 2 == 0;
}
public static bool 是奇数(int 值)
{
return 值 % 2 != 0;
}
public static int 随机数(int 最小值, int 最大值)
{
Random random = new Random();
return random.Next(最小值, 最大值 + 1);
}
public static double 随机小数()
{
Random random = new Random();
return random.NextDouble();
}
public static int 百分数(int 分子, int 分母)
{
if (分母 == 0) return 0;
return (分子 * 100) / 分母;
}
public static double 百分数(double 分子, double 分母)
{
if (分母 == 0) return 0;
return (分子 * 100) / 分母;
}
public static bool 在范围内(int 值, int 最小值, int 最大值)
{
return 值 >= 最小值 && 值 <= 最大值;
}
public static bool 在范围内(double 值, double 最小值, double 最大值)
{
return 值 >= 最小值 && 值 <= 最大值;
}
public static double 弧度到角度(double 弧度)
{
return 弧度 * 180 / Math.PI;
}
public static double 角度到弧度(double 角度)
{
return 角度 * Math.PI / 180;
}
public static double 取余数(double 值, double 除数)
{
return 值 % 除数;
}
public static decimal 取余数(decimal 值, decimal 除数)
{
return 值 % 除数;
}
public static string 格式化数字(double 值, int 小数位数 = 2)
{
return 值.ToString($"F{小数位数}");
}
public static string 格式化为货币(double 值)
{
return 值.ToString("C");
}
public static string 格式化为百分数(double 值)
{
return 值.ToString("P");
}
public static string 补零(int 值, int 总长度)
{
return 值.ToString().PadLeft(总长度, '0');
}
public static string 数字到中文大写(string 数字)
{
if (string.IsNullOrEmpty(数字)) return "";
string result = "";
string[] units = { "", "十", "百", "千", "万", "十万", "百万", "千万", "亿" };
string[] digits = { "零", "一", "二", "三", "四", "五", "六", "七", "八", "九" };
int len = 数字.Length;
for (int i = 0; i < len; i++)
{
int digit = int.Parse(数字.ToString());
int unitIndex = len - i - 1;
if (digit != 0)
{
result += digits[digit] + units[unitIndex];
}
else if (i == len - 1 || result.EndsWith("零"))
{
continue;
}
else
{
result += "零";
}
}
result = result.Replace("零万", "万");
result = result.Replace("零亿", "亿");
result = result.Replace("亿万", "亿");
if (result.EndsWith("零"))
result = result.Substring(0, result.Length - 1);
if (result.StartsWith("一十"))
result = result.Substring(1);
return result;
}
}