95

我有一个类似的字符串000000000100,我想将其转换为 1.00,反之亦然。

前导零将被删除,最后两位是小数。

我举更多例子:

000000001000 <=> 10.00
000000001005 <=> 10.05
000000331150 <=> 3311.50

下面是我正在尝试的代码,它给了我没有小数的结果:

amtf = string.Format("{0:0.00}", amt.TrimStart(new char[] {'0'}));
4

14 回答 14

81

将字符串转换为小数,然后除以 100 并应用货币格式字符串:

string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);

编辑以根据要求删除货币符号并转换为十进制。

于 2012-05-16T09:16:57.160 回答
67

您需要先将其转换为小数,然后使用货币格式对其进行格式化。

前任:

decimal decimalMoneyValue = 1921.39m;
string formattedMoneyValue = String.Format("{0:C}", decimalMoneyValue);

一个工作示例:https ://dotnetfiddle.net/soxxuW

于 2012-05-16T09:19:04.227 回答
52
decimal value = 0.00M;
value = Convert.ToDecimal(12345.12345);
Console.WriteLine(value.ToString("C"));
//OutPut : $12345.12
Console.WriteLine(value.ToString("C1"));
//OutPut : $12345.1
Console.WriteLine(value.ToString("C2"));
//OutPut : $12345.12
Console.WriteLine(value.ToString("C3"));
//OutPut : $12345.123
Console.WriteLine(value.ToString("C4"));
//OutPut : $12345.1234
Console.WriteLine(value.ToString("C5"));
//OutPut : $12345.12345
Console.WriteLine(value.ToString("C6"));
//OutPut : $12345.123450

控制台输出:

于 2016-07-25T10:19:14.013 回答
22

有用!

decimal moneyvalue = 1921.39m; 
string html = String.Format("Order Total: {0:C}", moneyvalue); 
Console.WriteLine(html);

输出

Order Total: $1,921.39
于 2014-06-27T03:55:01.903 回答
14

一旦您的字符串采用双/十进制格式,就可以将其转换为特定语言环境使用的正确格式

double amount = 1234.95;

amount.ToString("C") // whatever the executing computer thinks is the right fomat

amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ie"))    //  €1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("es-es"))    //  1.234,95 € 
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-GB"))    //  £1,234.95 

amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-au"))    //  $1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-us"))    //  $1,234.95
amount.ToString("C", System.Globalization.CultureInfo.GetCultureInfo("en-ca"))    //  $1,234.95
于 2016-03-24T16:35:15.120 回答
4
    string s ="000000000100";
    decimal iv = 0;
    decimal.TryParse(s, out iv);
    Console.WriteLine((iv / 100).ToString("0.00"));
于 2012-05-16T09:19:44.743 回答
4

试试这样简单

 var amtf = $"{Convert.ToDecimal(amt):#0.00}";
于 2016-04-22T11:39:22.503 回答
3
//Extra currency symbol and currency formatting: "€3,311.50":
String result = (Decimal.Parse("000000331150") / 100).ToString("C");

//No currency symbol and no currency formatting: "3311.50"
String result = (Decimal.Parse("000000331150") / 100).ToString("f2");
于 2012-05-16T09:19:29.607 回答
2

你也可以这样做:

string.Format("{0:C}", amt)
于 2013-07-10T19:18:49.713 回答
1

尝试这样的事情:

decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
于 2012-05-16T09:20:52.803 回答
0

首先将您的字符串解析为小数。

于 2012-05-16T09:17:22.447 回答
0
var tests = new[] {"000000001000", "000000001005", "000000331150"};
foreach (var test in tests)
{
    Console.WriteLine("{0} <=> {1:f2}", test, Convert.ToDecimal(test) / 100);
}

由于您没有要求货币符号,因此我使用“f2”而不是“C”

于 2012-05-16T09:23:14.427 回答
0

尝试

amtf =  amtf.Insert(amtf.Length - 2, ".");
于 2012-05-16T09:23:58.277 回答
0

就我而言,我使用这种字符串格式来显示不带符号的十进制值的货币。

字符串格式:

string.Format("{0:#,##0.00}", decimalValue)

例子:

var decimalNumbers = new decimal[] { 1M, 10M, 100M, 1000M,10000M,100000M,1000000M,1000000000M };          

foreach (var decimalNumber in decimalNumbers)
{
   Console.WriteLine(string.Format("{0:#,##0.00}", decimalNumber));
}
于 2021-05-23T16:22:00.793 回答