我有一个像000000000100
这样的字符串,我希望将其转换为1.00,反之亦然。
前导零将被删除,最后两位是小数。
我举了更多例子:
000000001000 <=> 10.00
000000001005 <=> 10.05
000000331150 <=> 3311.50
下面是我正在尝试的代码,它给我的结果没有小数:
amtf = string.Format("{0:0.00}", amt.TrimStart(new char[] {'0'}));
答案 0 :(得分:61)
将字符串转换为小数,然后除以100并应用货币格式字符串:
string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);
根据要求编辑删除货币符号,然后转换为十进制。
答案 1 :(得分:38)
首先需要将其转换为小数,然后使用货币格式对其进行格式化。
EX:
decimal decimalMoneyValue = 1921.39m;
string formattedMoneyValue = String.Format("{0:C}", decimalMoneyValue);
答案 2 :(得分:24)
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
控制台输出:
答案 3 :(得分:20)
有效!
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
输出
Order Total: $1,921.39
答案 4 :(得分:5)
将字符串设置为double / decimal以使其成为特定区域设置的正确格式
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
答案 5 :(得分:4)
string s ="000000000100";
decimal iv = 0;
decimal.TryParse(s, out iv);
Console.WriteLine((iv / 100).ToString("0.00"));
答案 6 :(得分:4)
尝试这样简单
var amtf = $"{Convert.ToDecimal(amt):#0.00}";
答案 7 :(得分:2)
//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");
答案 8 :(得分:1)
首先将字符串解析为小数。
答案 9 :(得分:1)
你也可以这样做:
string.Format("{0:C}", amt)
答案 10 :(得分:0)
尝试这样的事情:
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
答案 11 :(得分:0)
var tests = new[] {"000000001000", "000000001005", "000000331150"};
foreach (var test in tests)
{
Console.WriteLine("{0} <=> {1:f2}", test, Convert.ToDecimal(test) / 100);
}
由于你没有要求货币符号,我使用了“f2”代替“C”
答案 12 :(得分:0)
尝试
amtf = amtf.Insert(amtf.Length - 2, ".");
答案 13 :(得分: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));
}