[英]Conversion from Float to string in C#
When you convert 12345678.0 (float) into string, I don't know why it displays me "1.234568E+07 当您将12345678.0(浮点型)转换为字符串时,我不知道为什么它显示“ 1.234568E + 07”
I am using following code to display string 我正在使用以下代码显示字符串
String FinalPayement;
float amount = 12345678.0;
FinalPayement = Amount.ToString();
Does anybody know? 有人知道吗
First of all, if you don't use f
postfix for your literal, your literal will be double
instead of float
. 首先,如果您不对文字使用
f
后缀,则文字将是double
而不是float
。
var amount = 12345678.0f; //amount will be float
var amount = 12345678.0; // amount will be double.
Second of all, C# is case-sensitive. 其次,C#区分大小写。 That's mean
amount
and Amount
are not the same. 这意味着
amount
和Amount
不相同。 You should use amount.ToString()
instead of Amount.ToString()
. 您应该使用
amount.ToString()
代替Amount.ToString()
Third of all, as a solution, you can use custom numeric format like; 第三,作为解决方案,您可以使用自定义数字格式,例如;
float amount = 12345678.0f;
Console.WriteLine(amount.ToString(".0"));
Output will be like below; 输出将如下所示;
12345680.0
Here a demonstration
. 这里有
demonstration
。
To exert control over the formatting, pass a format string to the overload of ToString()
that accepts them. 要控制格式,请将格式字符串传递给接受它们的
ToString()
重载。 For instance: 例如:
float amount = 12345678.0f;
string finalPayment = amount.ToString("0.0");
String FinalPayement;
Decimal amount = 12345678.0;
FinalPayement = Amount.ToString();
instead of float try decimal... if you can change it then... otherwise other answers are quit good for you... 而不是浮动尝试十进制...如果您可以更改它...否则其他答案对您没有好处...
float amount = 12345678.0f;
Console.WriteLine(amount.ToString("f"));
float.Parse("41.00027357629127", CultureInfo.InvariantCulture.NumberFormat);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.