开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 38|回复: 0
收起左侧

[C#源码] C# 中文封装数值引用

[复制链接]
发表于 2 小时前 | 显示全部楼层 |阅读模式   湖南省怀化市
本帖最后由 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;
    }
}

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报QQ: 793400750,邮箱:wp@125.la
网站简介:精易论坛成立于2009年,是一个程序设计学习交流技术论坛,隶属于揭阳市揭东区精易科技有限公司所有。
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备2025452707号) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表