Files
YunTongJackYunTask/Reportapi/MyCode.Project.Infrastructure/Format/Money.cs
2025-07-04 09:50:02 +08:00

53 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
#region ToTwoPoint(,)
/// <summary>
/// 保留两位小数,四舍五入
/// </summary>
public static string ToTwoPoint(this decimal value)
{
return value.ToString("f2");
}
#endregion
#region CutDecimalWithN()
/// <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);
}
#endregion
}
}