转换字符串值与适当的小数点十进制 [英] Convert string value into decimal with proper decimal points
问题描述
我有存储字符串格式和放值;我想转换成十进制
i have value stored in string format & i want to convert into decimal.
例如:
我有 11.10
存储在字符串格式,当我尝试转换成十进制它给我 11.1
而不是 11.10
。
i have 11.10
stored in string format when i try to convert into decimal it give me 11.1
instead of 11.10
.
我尝试了通过下列方式
string getnumber="11.10";
decimal decinum=Convert.ToDecimal(getnumber);
我想这也是
i tried this also
decinum.ToString ("#.##");
但它返回的字符串,我想这个十进制。
but it returns string and i want this in decimal.
这可能是这种情况的解决方案?
what could be the solution for this?
推荐答案
由于已经评论 11.1
是相同的值 11.10
As already commented 11.1
is the same value as 11.10
decimal one=11.1;
decimal two=11.10;
Console.WriteLine(one == two);
将输出真正
在字符串方法中的#
格式意味着一个可选的数字,如果是0会剿(并且是有效的抑制 - 在 0
在 4.05
不会被抑制)。尝试
The #
formatter in the to string method means an optional digit and will supress if it is zero (and it is valid to suppress - the 0
in 4.05
wouldn't be suppressed). Try
decinum.ToString("0.00");
和你将看到 11.10
在理想情况下,你真的想使用类似
Ideally you actually want to use something like
string input="11.10";
decimal result;
if (decimal.TryParse(input,out result)) {
Console.WriteLine(result == 11.10);
} else {
// The string wasn't a decimal so do something like throw an error.
}
在上面的代码结束后,结果将是你想要的十进制和真将被输出到控制台
At the end of the above code, result will be the decimal you want and "true" will be output to the console.
这篇关于转换字符串值与适当的小数点十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!