将字符串转换为十进制奥秘 [英] Convert String to Decimal Mystery
问题描述
我面临使用C#.NET从字符串转换为十进制的问题。问题是我已经编写了以下代码,并且在开发Windows 7 .NET Framework 3.5系统上可以正常工作。但是,如果我在Windows 2003 Server .NET 3.5 Framework上移动应用程序,结果将有所不同。有人能理解那里发生了什么吗?
I'm facing a problem about the conversion from string to decimal using C# .NET. The problem is that I have wrote the following code and works fine on my development Windows 7 .NET Framework 3.5 system. But, if I move the application on the Windows 2003 Server .NET 3.5 Framework the result is different. Can anyone understand what happen there?
代码:
dec = Convert.ToDecimal(strDec);
Windows 7结果:85.00
Windows 2003结果:8500
Windows 7 result: 85.00 Windows 2003 result: 8500
推荐答案
可能是服务器上的区域和区域性设置是用,
设置为十进制,和。
作为数字分组。请参阅控制面板/区域和区域性设置。
It may be that the region and culture settings on your server are set with ,
as decimal, and .
as digit grouping. See Control Panel/region and culture settings.
如果此数字字符串是由您的应用程序生成的,则我将使用 CultureInfo.InvariantCulture $ c
If this numeric string is generated by your application, I would use CultureInfo.InvariantCulture
in both places.
Decimal dec = 85.0m;
string s = dec.ToString (CultureInfo.InvariantCulture);
decimal.Parse(s, CultureInfo.InvariantCulture);
这篇关于将字符串转换为十进制奥秘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!