我有一个类似的字符串000000000100
,我想将其转换为 1.00,反之亦然。
前导零将被删除,最后两位是小数。
我举更多例子:
000000001000 <=> 10.00
000000001005 <=> 10.05
000000331150 <=> 3311.50
下面是我正在尝试的代码,它给了我没有小数的结果:
amtf = string.Format("{0:0.00}", amt.TrimStart(new char[] {'0'}));
我有一个类似的字符串000000000100
,我想将其转换为 1.00,反之亦然。
前导零将被删除,最后两位是小数。
我举更多例子:
000000001000 <=> 10.00
000000001005 <=> 10.05
000000331150 <=> 3311.50
下面是我正在尝试的代码,它给了我没有小数的结果:
amtf = string.Format("{0:0.00}", amt.TrimStart(new char[] {'0'}));
将字符串转换为小数,然后除以 100 并应用货币格式字符串:
string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);
编辑以根据要求删除货币符号并转换为十进制。
您需要先将其转换为小数,然后使用货币格式对其进行格式化。
前任:
decimal decimalMoneyValue = 1921.39m;
string formattedMoneyValue = String.Format("{0:C}", decimalMoneyValue);
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
控制台输出:
有用!
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
输出
Order Total: $1,921.39
一旦您的字符串采用双/十进制格式,就可以将其转换为特定语言环境使用的正确格式
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
string s ="000000000100";
decimal iv = 0;
decimal.TryParse(s, out iv);
Console.WriteLine((iv / 100).ToString("0.00"));
试试这样简单
var amtf = $"{Convert.ToDecimal(amt):#0.00}";
//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");
你也可以这样做:
string.Format("{0:C}", amt)
尝试这样的事情:
decimal moneyvalue = 1921.39m;
string html = String.Format("Order Total: {0:C}", moneyvalue);
Console.WriteLine(html);
首先将您的字符串解析为小数。
var tests = new[] {"000000001000", "000000001005", "000000331150"};
foreach (var test in tests)
{
Console.WriteLine("{0} <=> {1:f2}", test, Convert.ToDecimal(test) / 100);
}
由于您没有要求货币符号,因此我使用“f2”而不是“C”
尝试
amtf = amtf.Insert(amtf.Length - 2, ".");
就我而言,我使用这种字符串格式来显示不带符号的十进制值的货币。
字符串格式:
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));
}