48 lines
1.3 KiB
C#
Raw Normal View History

2025-04-21 14:10:27 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyCode.Project.Infrastructure.Extensions;
namespace MyCode.Project.Infrastructure.Format
{
public static class Money
{
/// <summary>
/// 保留两位小数,四舍五入
/// </summary>
public static string ToTwoPoint(this decimal value)
{
return value.ToString("f2");
}
/// <summary>
/// 保留两位小数,不四舍五入
/// </summary>
/// <param name="d"></param>
/// <param name="n"></param>
/// <returns></returns>
public static decimal CutDecimalWithN(decimal d, int n)
{
string strDecimal = d.ToString();
int index = strDecimal.IndexOf(".");
if (index == -1 || strDecimal.Length < index + n + 1)
{
strDecimal = string.Format("{0:F" + n + "}", d);
}
else
{
int length = index;
if (n != 0)
{
length = index + n + 1;
}
strDecimal = strDecimal.Substring(0, length);
}
return Decimal.Parse(strDecimal);
}
}
}