如何在整数中放置小数点
How to put decimal point in a whole number
我正在从银行读取带有一串数字的文本文件。 0000000010050
,这个字符串的金额是$100.50,但是有没有办法在字符串中插入小数点呢?这是我的代码。
string amount = 0000000010050;
string f_amount = "";
string ff_amount = "";
decimal d_amount = 0;
f_amount = amount.Trim();
d_amount = int.Parse(f_amount.TrimStart('0')); // this part removes the zeros and the output is 10050.
ff_amount = string.Format("{0:0,0.00}", d_amount); // this line outputs 10050.00
如何使输出看起来像这样100.50
?
类似这样的事情(让我们考虑 CultureInfo
)
using System.Globalization;
...
string amount = "0000000010050";
amount = amount
.Insert(amount.Length - CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits,
CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator)
.TrimStart('0');
先把字符串转成十进制,这样应用string.Format
string.Format("{0:#.00}", Convert.ToDecimal(bankString) / 100);
这将给出结果 100.50
我正在从银行读取带有一串数字的文本文件。 0000000010050
,这个字符串的金额是$100.50,但是有没有办法在字符串中插入小数点呢?这是我的代码。
string amount = 0000000010050;
string f_amount = "";
string ff_amount = "";
decimal d_amount = 0;
f_amount = amount.Trim();
d_amount = int.Parse(f_amount.TrimStart('0')); // this part removes the zeros and the output is 10050.
ff_amount = string.Format("{0:0,0.00}", d_amount); // this line outputs 10050.00
如何使输出看起来像这样100.50
?
类似这样的事情(让我们考虑 CultureInfo
)
using System.Globalization;
...
string amount = "0000000010050";
amount = amount
.Insert(amount.Length - CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalDigits,
CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator)
.TrimStart('0');
先把字符串转成十进制,这样应用string.Format
string.Format("{0:#.00}", Convert.ToDecimal(bankString) / 100);
这将给出结果 100.50