添加项目文件。
This commit is contained in:
56
Extensions/StringExtension.cs
Normal file
56
Extensions/StringExtension.cs
Normal file
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
|
||||
namespace ExtensionMethods
|
||||
{
|
||||
public static class StringExtension
|
||||
{
|
||||
/// <summary>
|
||||
/// 四舍五入保留2位小数
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ToDecimalR(this string obj)
|
||||
{
|
||||
return Math.Round(obj.ToDecimal(), 2, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 四舍五入保留2位小数
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ToDecimalR(this object obj)
|
||||
{
|
||||
return Math.Round(obj.ToDecimal(), 2, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
public static decimal ToDecimalR(this decimal obj)
|
||||
{
|
||||
return Math.Round(obj, 2, MidpointRounding.AwayFromZero);
|
||||
}
|
||||
|
||||
public static decimal ToDecimal(this object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return 0;
|
||||
var str = obj.ToString().Trim();
|
||||
|
||||
decimal result;
|
||||
decimal.TryParse(str, out result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static decimal ToDecimal(this string obj)
|
||||
{
|
||||
decimal result;
|
||||
decimal.TryParse(obj.Trim(), out result);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user