12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- using FL.Network;
- using System;
- 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 FormatNumber(this long number, int decimals = 1)
- {
- const int million = 1000000;
- const int thousand = 1000;
- var format = $"N{decimals}";//$"{{0:N{decimals}}}";
- if (number >= million)
- {
- return ((double)number / million).ToString(format) + MILLION;
- }
- if (number >= thousand * 10)
- {
- return ((double)number / thousand).ToString(format) + THOUSAND;
- }
- return number.ToString();
- }
- /// <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>
- /// 数据库万分比字段转真实值以显示百分比显示(最多保留小数点后2位,小数部分为零时不显示)
- /// </summary>
- /// <param name="val"></param>
- /// <returns></returns>
- public static string ToRealFloatPercentage(this int val)
- {
- return val > 0 ? (val * 0.0001f).ToString("G2") + "%" : "0%";
- }
- /// <summary>
- /// 数据库万分比字段转真实值以显示百分比显示(最多保留小数点后2位,小数部分为零时不显示)
- /// </summary>
- /// <param name="val"></param>
- /// <returns></returns>
- public static string ToRealDoublePercentage(this long val)
- {
- return val > 0 ? (val * 0.0001f).ToString("G2") + "%" : "0%";
- }
- }
- }
|