You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
851 B

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Utils.Datas
{
public class NumToStringUtil
{
/// <summary>
/// 小数位最多N位
/// </summary>
public static string GetPointAtMost(decimal num, int places = 2)
{
var s = GetStandardPoint(num, places);
if (!s.EndsWith("0"))
return s;
int zeroLength = 1;
for (int i = 1; i < places; i++) {
if (s.ElementAt(s.Length - zeroLength - 1) != '0')
break;
zeroLength++;
}
if (zeroLength == places)
return s.Substring(0, s.Length - zeroLength - 1);
return s.Substring(0, s.Length - zeroLength);
}
/// <summary>
/// N位小数
/// </summary>
public static string GetStandardPoint(decimal num, int places = 2)
{
if (places < 1)
places = 2;
return num.ToString("F" + places);
}
}
}