Files
WMS-Api/src/WMS.Web.Core/NumericalProcess.cs
2023-10-18 10:21:21 +08:00

64 lines
2.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.Text;
namespace WMS.Web.Core
{
/// <summary>
/// 数值处理
/// </summary>
public static class NumericalProcess
{
/// <summary>
/// Decimal 数值精度转换
/// </summary>
/// <param name="number"> decimal数值</param>
/// <param name="precision">精度值</param>
/// <param name="RoundoffType"> 舍入类型1为进位2为舍位3为四舍五入</param>
/// <returns>decimal数值</returns>
public static decimal DecimalPrecision(this decimal? number, int? precision, int? RoundoffType)
{
if (RoundoffType == 1)
return Decimal.Round(number ?? 0, precision ?? 0, MidpointRounding.ToPositiveInfinity);
else if (RoundoffType == 2)
return Decimal.Round(number ?? 0, precision ?? 0, MidpointRounding.ToZero);
else if (RoundoffType == 3)
return Decimal.Round(number ?? 0, precision ?? 0, MidpointRounding.AwayFromZero);
return number ?? 0;
}
/// <summary>
/// Decimal 数值精度转换
/// </summary>
/// <param name="number"> decimal数值</param>
/// <param name="precision">精度值</param>
/// <param name="RoundoffType"> 舍入类型1为进位2为舍位3为四舍五入</param>
/// <returns>decimal数值</returns>
public static decimal DecimalPrecision(this decimal number, int? precision, int? RoundoffType)
{
if (RoundoffType == 1)
return Decimal.Round(number, precision ?? 0, MidpointRounding.ToPositiveInfinity);
else if (RoundoffType == 2)
return Decimal.Round(number, precision ?? 0, MidpointRounding.ToZero);
else if (RoundoffType == 3)
return Decimal.Round(number, precision ?? 0, MidpointRounding.AwayFromZero);
return number;
}
/// <summary>
/// Decimal 去掉后面无效的0
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string DecimalToStringNoZero(this decimal? data)
{
return (data ?? 0).ToString("#0.##########");
}
public static string DecimalToStringNoZero(this decimal data)
{
return data.ToString("#0.##########");
}
}
}