C#.net 格式化货币
直接显示带浮点数千分位的数据:
例如:12,345.00
string.Format("{0:N2}", fmoney);
注意:这里的fmoney必须是decimal型的,若fmoney传入的是字符串,那么结果不会带千分位
第一种:(输入Float格式数字,将其转换为货币表达方式)
/// <summary>
/// 输入Float格式数字,将其转换为货币表达方式
/// </summary>
/// <param name="ftype">货币表达类型:0=带¥的货币表达方式;1=不带¥的货币表达方式;其它=带¥的货币表达方式</param>
/// <param name="fmoney">传入的int数字</param>
/// <returns>返回转换的货币表达形式</returns>
public string Rmoney(int ftype, double fmoney)
{
string _rmoney;
try
{
switch (ftype)
{
case 0:
_rmoney = string.Format("{0:C2}", fmoney);
break;
case 1:
_rmoney = string.Format("{0:N2}", fmoney);
break;
default:
_rmoney = string.Format("{0:C2}", fmoney);
break;
}
}
catch
{
_rmoney = "";
}
return _rmoney;
}
第二种:
using System;
using System.Globalization;
public class TestClass
{
public static void Main()
{
int i = 100;
// Creates a CultureInfo for English in Belize.
CultureInfo bz = new CultureInfo( "en-BZ ");
// Displays i formatted as currency for the bz.
Console.WriteLine(i.ToString( "c ", bz));
// Creates a CultureInfo for English in the U.S.
CultureInfo us = new CultureInfo( "en-US ");
// Display i formatted as currency for us.
Console.WriteLine(i.ToString( "c ", us));
// Creates a CultureInfo for Danish in Denmark.
CultureInfo dk = new CultureInfo( "da-DK ");
// Displays i formatted as currency for dk.
Console.WriteLine(i.ToString( "c ", dk));
}
}
此代码产生下列输出:
BZ$100.00
$100.00
kr100,00