简体   繁体   English

使用正确的小数点将字符串值转换为十进制

[英]Convert string value into decimal with proper decimal points

i have value stored in string format & i want to convert into decimal. 我有以字符串格式存储的值,我想转换为十进制。

ex: 例如:

i have 11.10 stored in string format when i try to convert into decimal it give me 11.1 instead of 11.10 . 当我尝试转换为十进制时,我有11.10以字符串格式存储它给我11.1而不是11.10

I tried it by following way 我通过以下方式尝试了它

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? 这可能是什么解决方案?

As already commented 11.1 is the same value as 11.10 已经评论过11.111.10值相同

decimal one=11.1;
decimal two=11.10;
Console.WriteLine(one == two);

Will output true 将输出为true

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). #在到串格式化方法是指任选的数字,并且如果它是零将剿(它是有效的抑制-将04.05不会被抑制)。 Try 尝试

decinum.ToString("0.00"); 

And you will see the string value of 11.10 你会看到字符串值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. 在上面的代码的末尾,结果将是您想要的小数,并且“true”将输出到控制台。

this will work perfectly 这将完美地运作

string getnumber = "11.10";
decimal decinum = Math.Round(Convert.ToDecimal(getnumber), 2);

A decimal datatype stores a value . decimal数据类型存储 The value of 11.1 is identical to that of 11.10 - mathemtically, they're the exact same number, so it's behaving properly. 11.1的值与11.10的值相同 - 在数学上,它们是完全相同的数字,所以它的行为正常。

What you seem to want is to display the number as 11.10, but that's up to the code that displays it - I don't know if you're writing to log file, displaying on a web-page or any other format - but that is independent of the internal representation of the decimal datatype. 你似乎想要的是将数字显示为11.10,但这取决于显示它的代码 - 我不知道你是写入日志文件,显示在网页上还是任何其他格式 - 但是独立于decimal数据类型的内部表示。

there is no solution, This is expected behaviour. 没有解决方案,这是预期的行为。 11.10 in string = 11.1 in number 11.10 in string = 11.1 in number

you cant store zeros on the decimal part, otherwise 11.10 would be different than 11.100 , which is true if you are talking about strings but not if you are talking about numbers. 你不能在小数部分存储零,否则11.10将不同于11.100 ,如果你在谈论字符串,这是真的,但如果你在谈论数字则不然。

I think your problem is on a presentation level only. 我认为您的问题仅限于演示级别。 Why dont you explain better what you want to do. 为什么不更好地解释你想做什么。

11.10 expressed as a decimal is 11.1, just like it is 11.100000000000000000000000000000000000. 11.10表示为小数是11.1,就像它是11.100000000000000000000000000000000000。

For mathematical processes, don't worry about how it displays itself. 对于数学过程,不要担心它如何显示自己。 If you are displaying the value then converting it into a string is no biggie either. 如果您正在显示该值,那么将其转换为字符串也不是很重要。 Remember that 记住这一点

decinum.ToString ("#.##");

is returning a string (from the 'ToString' function) and not converting the 'decinum' to a string. 返回一个字符串(来自'ToString'函数)而不是将'decinum'转换为字符串。

    string getnumber = "11.10";
    double decinum = double.Parse(getnumber);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM