c#實現的四舍五入和大小寫轉換函數
2007-10-26 09:07
134 查看
public class NumberFun
{
/// <summary>
/// 四舍五入
/// </summary>
/// <param name="d">要四舍五入的數</param>
/// <param name="i">要保留的小數位數</param>
/// <returns></returns>
public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);//截取需要的位數
}
if (poststr.Length <= 2)
{
poststr = poststr + "0";
}
return Double.Parse(prestr + "." + poststr);
}
/// <summary>
/// 將商品金額小寫轉換為大寫
/// </summary>
/// <param name="AmountInFigures">小寫金額</param>
/// <returns></returns>
public static string GetAmountInWords(string AmountInFigures)
{
string[] scale = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
string[] capital = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] strs = AmountInFigures.Split('.');
string WholeNumber = strs[0];
string DecimalFraction = strs.Length == 2 ? strs[1] : null;
string info = null;
for (int i = WholeNumber.Length; i > 0; i--)
{
int data = int.Parse(WholeNumber[WholeNumber.Length - i].ToString());
info += capital[data];
info += scale[i + 1];
}
if (DecimalFraction != null)
{
if (DecimalFraction.Length == 1)
DecimalFraction += "0";
for (int i = DecimalFraction.Length; i > 0; i--)
{
int data = int.Parse(DecimalFraction[DecimalFraction.Length - i].ToString());
info += capital[data];
info += scale[i - 1];
}
}
return info;
}
}
{
/// <summary>
/// 四舍五入
/// </summary>
/// <param name="d">要四舍五入的數</param>
/// <param name="i">要保留的小數位數</param>
/// <returns></returns>
public static double Round(double d, int i)
{
if (d >= 0)
{
d += 5 * Math.Pow(10, -(i + 1));
}
else
{
d += -5 * Math.Pow(10, -(i + 1));
}
string str = d.ToString();
string[] strs = str.Split('.');
int idot = str.IndexOf('.');
string prestr = strs[0];
string poststr = strs[1];
if (poststr.Length > i)
{
poststr = str.Substring(idot + 1, i);//截取需要的位數
}
if (poststr.Length <= 2)
{
poststr = poststr + "0";
}
return Double.Parse(prestr + "." + poststr);
}
/// <summary>
/// 將商品金額小寫轉換為大寫
/// </summary>
/// <param name="AmountInFigures">小寫金額</param>
/// <returns></returns>
public static string GetAmountInWords(string AmountInFigures)
{
string[] scale = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾", "佰", "仟" };
string[] capital = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };
string[] strs = AmountInFigures.Split('.');
string WholeNumber = strs[0];
string DecimalFraction = strs.Length == 2 ? strs[1] : null;
string info = null;
for (int i = WholeNumber.Length; i > 0; i--)
{
int data = int.Parse(WholeNumber[WholeNumber.Length - i].ToString());
info += capital[data];
info += scale[i + 1];
}
if (DecimalFraction != null)
{
if (DecimalFraction.Length == 1)
DecimalFraction += "0";
for (int i = DecimalFraction.Length; i > 0; i--)
{
int data = int.Parse(DecimalFraction[DecimalFraction.Length - i].ToString());
info += capital[data];
info += scale[i - 1];
}
}
return info;
}
}
相关文章推荐
- 转:c# 简单又好用的四舍五入方法
- C#實現執行dos命令的方法
- C#使用round函数四舍五入的方法
- C#中Math.Round()实现中国式四舍五入
- C#中Math.Round()实现中国式四舍五入
- c# 三种取整方法 向上取整 向下取整 四舍五入
- c#中的浮点型转整形的舍取 四舍五入和银行家舍入实现代码
- C# 實現文件壓縮-- 背景:服務器Log.txt 過多,佔用過多硬盤空間,壓縮備份后節省空間資源
- C# 小数点后保留两位小数,四舍五入的函数及使用方法
- C#使用Math.Round()四舍五入
- C#中的四舍五入
- C# 四舍五入
- C#中 decimal 的四舍五入
- C#中的四舍五入
- Windows Server 2003 C#關機程序實現(用CSC.exe 2.0V進行編譯即可/Logo.ico要有啊)
- C# decimal保留指定的小数位数,不四舍五入
- c# 的四舍五入
- 关于c#double型数据四舍五入的问题
- 关于c#的四舍五入问题
- C#不管什么四舍五入,只要是小数取整就得加1