您的位置:首页 > 编程语言 > C#

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: