C# 使用适当的小数点将字符串值转换为十进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12174273/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 22:20:56  来源:igfitidea点击:

Convert string value into decimal with proper decimal points

c#decimal

提问by sharad

i have value stored in string format & i want to convert into decimal.

我有以字符串格式存储的值 & 我想转换为十进制。

ex:

前任:

i have 11.10stored in string format when i try to convert into decimal it give me 11.1instead 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?

这可能是什么解决方案?

采纳答案by Bob Vale

As already commented 11.1is the same value as 11.10

正如已经评论11.1的那样与价值相同11.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 0in 4.05wouldn't be suppressed). Try

#to string 方法中的格式化程序表示一个可选数字,如果它为零则将被抑制(并且抑制是有效的 - 0in4.05不会被抑制)。尝试

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”将输出到控制台。

回答by Avner Shahar-Kashtan

A decimaldatatype stores a value. The value of 11.1 is identical to that of 11.10 - mathemtically, they're the exact same number, so it's behaving properly.

一个decimal数据类型存储。11.1 的值与 11.10 的值相同 - 从数学上讲,它们是完全相同的数字,因此它的行为正常。

What you seem to want is to displaythe 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 decimaldatatype.

您似乎想要的是将数字显示为 11.10,但这取决于显示它的代码-我不知道您是在写入日志文件、显示在网页上还是任何其他格式-但是独立于decimal数据类型的内部表示。

回答by Diego

there is no solution, This is expected behaviour. 11.10in string = 11.1in number

没有解决方案,这是预期的行为。 11.10字符串 =11.1数字

you cant store zeros on the decimal part, otherwise 11.10would 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.

我认为您的问题仅在演示级别上。你为什么不更好地解释你想做什么。

回答by SeanCocteau

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' 转换为字符串。

回答by Sora

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

回答by Ankush Jain

this will work perfectly

这将完美地工作

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