123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- using System;
- using XGame.Database;
- namespace FL
- {
- public static class StringUtils
- {
- /// <summary>
- /// 毫秒转指定格式的时间字符串
- /// </summary>
- /// <param name="milliseconds"></param>
- /// <param name="format">字符串格式, 例: @"mm\:ss"</param>
- /// <returns></returns>
- public static string ToTimeString(this long milliseconds, string format)
- {
- var timeData = TimeSpan.FromMilliseconds(milliseconds);
- return timeData.ToString(format);
- }
- private const string MILLION = "M";
- private const string THOUSAND = "K";
- /// <summary>
- /// 货币、战力数字转字符串
- /// >=百万,以M结尾
- /// >=万,以K结尾
- /// </summary>
- /// <param name="number"></param>
- /// <param name="decimals">保留小数点位数</param>
- /// <returns></returns>
- public static string ToNumberString(this long number, int decimals = 2)
- {
- const int million = 1000000;
- const int thousand = 1000;
- var format = $"F{decimals}";//$"{{0:N{decimals}}}";
- if (number >= million)
- {
- if (number % million == 0)
- return (number / million).ToString() + MILLION;
- return ((double)number / million).ToString(format) + MILLION;
- }
- if (number >= thousand * 10)
- {
- if (number % thousand == 0)
- return (number / thousand).ToString() + THOUSAND;
- return ((double)number / thousand).ToString(format) + THOUSAND;
- }
- return number.ToString();
- }
- /// <summary>
- /// 数据库万分比字段转真实值以显示百分比显示(最多保留小数点后2位,小数部分为零时不显示)
- /// </summary>
- /// <param name="val"></param>
- /// <param name="decimals">保留小数点位数</param>
- /// <returns></returns>
- public static string ToPercentString(this int val, int decimals = 2)
- {
- const int TenThousand = 10000; // 万
- if (val % TenThousand < 50)
- { // 小数点4舍5入,小于0.005,尾数为xx.00%
- return (val * 0.0001f).ToString("P0");
- }
- return (val * 0.0001f).ToString($"P{decimals}");
- }
- /// <summary>
- /// 数据库万分比字段转真实值以显示百分比显示(最多保留小数点后2位,小数部分为零时不显示)
- /// </summary>
- /// <param name="val"></param>
- /// <param name="decimals">保留小数点位数</param>
- /// <returns></returns>
- public static string ToPercentString(this long val, int decimals = 2)
- {
- const int TenThousand = 10000; // 万
- if (val % TenThousand < 50)
- { // 小数点4舍5入,小于0.005,尾数为xx.00%
- return (val * 0.0001f).ToString("P0");
- }
- return (val * 0.0001f).ToString($"P{decimals}");
- }
- /// <summary>
- /// 时间戳转换成 xxxx-xx-xx xx:xx格式
- /// </summary>
- /// <param name="milliseconds"></param>
- /// <returns></returns>
- public static string ToTimeYMDHM(this long milliseconds)
- {
- DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeSeconds(milliseconds);
- string yearMonthDay = dateTime.ToString("yyyy-MM-dd hh:mm");
- return yearMonthDay;
- }
- /// <summary>
- /// 获取属性类型值
- /// </summary>
- /// <param name="attributeInfo"></param>
- /// <param name="attrVal"></param>
- /// <returns></returns>
- public static string GetAttrValueDesc(AttrDescTable attributeInfo, int attrVal)
- {
- return (attributeInfo.Num_type == 1 || attributeInfo.Id == (int)EAttributeType.AtkSpeed) ? attrVal.ToPercentString() :
- (attrVal == 0 ? "0" : attrVal.ToString());
- }
- }
- }
|