简体   繁体   English

将字符串转换为十进制忽略点

[英]Convert string to decimal ignore point

today iv notice that all the place that im using decimal first thing is in the grid show comma instead of point the second problame is when i edit or insert number with point it ignore the point 今天iv请注意,我使用小数点第一位的所有位置都是在网格中显示逗号而不是点,第二个问题是当我编辑或插入带有点的数字时,它忽略了该点

like i enterd "2.5" i got it as 25 就像我输入“ 2.5”一样,我得到25

thats like example code: 多数民众赞成在示例代码:

decimal a = Convert.ToDecimal("2.5");

the resoult of a is 25. a的结果是25。

this problame is happening on the iis and also in my local machine. 这个问题发生在iis以及我的本地计算机上。

it all over the place , how can i fix it ? 它到处都是,我该如何解决?

What culture are you currently on? 您目前所处的文化是什么? This should do the work: 这应该做的工作:

decimal a = Convert.ToDecimal("2.5", new CultureInfo("en-US"));

Also, for the comma showing: 另外,对于逗号显示:

string s = a.ToString(new CultureInfo("en-US"));

You can probably fix it by changing your default culture, or by forcing the use of a specific culture in the conversion calls (a) . 您可能可以通过更改默认区域性或在转换调用(a)中强制使用特定区域性来修复它。

Our wonderful, yet somehow slightly disturbing European cousins have gotten the decimal point and comma mixed up at some point in the past, so that thirty-five-thousand-and-seven-point-two-five is written as: 在过去的某个时候,我们出色的,但又有点令人不安的欧洲表亲已经达到了小数点和逗号混合的状态,因此三万五千七点二十五写成:

35.007,25

So what's almost certainly happening is that your culture settings are throwing away the thousands separator (regardless of where they are) so that "2.5" is 25. This means that a number like two-and-half-thousand ( 2,500 ) would almost certainly become two-and-a-half. 因此,几乎可以肯定的是,您的文化设置将丢弃千位分隔符(无论它们在何处),因此“ 2.5” 25。这意味着,几乎可以肯定的是,两分之二( 2,500 )变成两个半。


(a) : In fact, you probably shouldn't force a specific culture in the conversion call, since the whole point of cultures is to adapt to people's needs. (a) :实际上,您可能不应该在转换通话中强加特定的文化,因为文化的全部重点就是要适应人们的需求。 The Europeans thinks little enough of US bods already without giving them more reasons :-) What you're seeing is exactly what's supposed to be happening. 欧洲人认为美国的尸体已经很少,没有给他们更多的理由了:-)您所看到的正是应该发生的事情。

If you need a specific culture, configure your machine that way, and let your code use the default. 如果您需要特定的区域性,请以这种方式配置计算机,并让您的代码使用默认值。

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

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