Files
zszq-trs/YLErpDAL/Helpers/StringHelper.cs
T
2024-05-09 14:06:26 +08:00

54 lines
1.9 KiB
C#

namespace YLErp.Helpers
{
public static class StringHelper
{
public static bool IsNullOrWhiteSpace(this string str)
{
return string.IsNullOrWhiteSpace(str);
}
public static string GetFixedStr(string desc1, string desc2, int total1 = 40, int total2 = 20)
{
int remainLength = total1 - System.Text.Encoding.Default.GetBytes(desc1).Length;
int remainLength2 = total2 - System.Text.Encoding.Default.GetBytes(desc2).Length;
return desc1 + "".PadRight(remainLength, " "[0]) + desc2 + "".PadRight(remainLength2, " "[0]);
}
/// <summary>
/// 根据targetStr长度设置
/// </summary>
/// <param name="desc1"></param>
/// <returns></returns>
public static string GetFixedStr2(string desc1, string targetStr)
{
int targetLength = System.Text.Encoding.Default.GetBytes(targetStr).Length;
int useLength = System.Text.Encoding.Default.GetBytes((desc1 + "")).Length;
int remainLength = targetLength - useLength;
if (remainLength < 0)
{
remainLength = 0;
}
return desc1 + "".PadRight(remainLength, " "[0]);
}
/// <summary>
/// 以精确模式转换浮点数为特定小数精度的字符串,使用四舍五入模式,
/// 但111111111111.2249999999999999需要转为111111111111.22而不是111111111111.23
/// </summary>
public static string ToFixedPrecise(double? value, int decimalSize = 2)
{
if (value == null)
{
return string.Empty;
}
if (decimalSize < 1)
{
return Math.Floor(value.Value).ToString("0");
}
var decExp = Math.Pow(10, decimalSize + 1);
return (Math.Floor(value.Value * decExp) / decExp).ToString("f" + decimalSize);
}
}
}