Skip to content

Commit

Permalink
DecimalUtility及中文大写数字
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimmey-Jiang committed Jan 5, 2018
1 parent ff08de3 commit bfe1fb0
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 0 deletions.
Binary file modified Utility基础类大全/.vs/Common.Utility/v15/.suo
Binary file not shown.
Binary file not shown.
Binary file not shown.
1 change: 1 addition & 0 deletions Utility基础类大全/Common.Utility.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<Compile Include="CSV文件转换\CsvHelper.cs" />
<Compile Include="DataTable转实体\DataTableExtensions.cs" />
<Compile Include="DataTable转实体\ListExtensions.cs" />
<Compile Include="DecimalUtility及中文大写数字\DecimalUtility.cs" />
<Compile Include="EcanConvertToCh.cs" />
<Compile Include="FormulaExpress.cs" />
<Compile Include="GridViewHelper.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Common.Utilities
{
public static class DecimalUtility
{
public static Decimal? ParseToDecimalValue(object decimalObj)
{
if (decimalObj == null) return null;
Decimal decValue;
if (!Decimal.TryParse(decimalObj.ToString(), out decValue)) return null;
return decValue;
}

/// <summary>
/// 转中文大写数字
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static string ConvertNumToZHUpperCase(decimal value)
{
string[] numList = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] unitList = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟" };

decimal money = value;
if (money == 0)
{
return "零元整";
}

StringBuilder strMoney = new StringBuilder();
//只取小数后2位



string strNum = decimal.Truncate(money * 100).ToString();

int len = strNum.Length;
int zero = 0;
for (int i = 0; i < len; i++)
{
int num = int.Parse(strNum.Substring(i, 1));
int unitNum = len - i - 1;

if (num == 0)
{
zero++;
if (unitNum == 2 || unitNum == 6 || unitNum == 10)
{
if (unitNum == 2 || zero < 4)
strMoney.Append(unitList[unitNum]);
zero = 0;
}
}
else
{

if (zero > 0)
{
strMoney.Append(numList[0]);
zero = 0;
}
strMoney.Append(numList[num]);
strMoney.Append(unitList[unitNum]);
}

}
if (zero > 0)
strMoney.Append("整");

return strMoney.ToString();
}

/// <summary>
/// 截取指定位数
/// </summary>
/// <param name="d"></param>
/// <param name="s"></param>
/// <returns></returns>
public static decimal ToFixed(decimal d, int s)
{
decimal sp = Convert.ToDecimal(Math.Pow(10, s));
return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
}

/// <summary>
/// 截取指定位数
/// </summary>
/// <param name="d"></param>
/// <param name="s"></param>
/// <returns></returns>
public static double ToFixed(double d, int s)
{
double sp = Math.Pow(10, s);
return Math.Truncate(d) + Math.Floor((d - Math.Truncate(d)) * sp) / sp;
}
}
}
Binary file modified Utility基础类大全/bin/Debug/DotNet.Utilities.dll
Binary file not shown.
Binary file modified Utility基础类大全/bin/Debug/DotNet.Utilities.pdb
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
4601c699269147baa49c7900835656b5a1c40a49
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ G:\github合集\Common.Utility\Utility基础类大全\bin\Debug\Common.Logging.C
G:\github合集\Common.Utility\Utility基础类大全\bin\Debug\NPOI.xml
G:\github合集\Common.Utility\Utility基础类大全\bin\Debug\Quartz.xml
G:\github合集\Common.Utility\Utility基础类大全\obj\Debug\Common.Utility.csprojResolveAssemblyReference.cache
G:\github合集\Common.Utility\Utility基础类大全\obj\Debug\Common.Utility.csproj.CoreCompileInputs.cache
Binary file not shown.
Binary file not shown.
Binary file modified Utility基础类大全/obj/Debug/DotNet.Utilities.dll
Binary file not shown.
Binary file modified Utility基础类大全/obj/Debug/DotNet.Utilities.pdb
Binary file not shown.

0 comments on commit bfe1fb0

Please sign in to comment.