简体   繁体   English

使用相同的 CultureInfo 时,不同计算机上的格式不同,为什么?

[英]Different formatting on different computers when using the same CultureInfo, why?

I'm having an issue when using CultureInfo .我在使用CultureInfo时遇到问题。

This is basically my code:这基本上是我的代码:

Console.WriteLine(0.5.ToString("P2", new CultureInfo("en-US")));

When running it, this is what I get on two different computers:运行它时,这是我在两台不同的计算机上得到的:

As you can notice, the formatting isn't the same.如您所见,格式不一样。 The Australia VPS ( running Windows Server 2016) having a space before the percent symbol compared to my computer (Windows 10).与我的计算机(Windows 10)相比,澳大利亚 VPS(运行 Windows Server 2016)在百分号之前有一个空格。

Why that?为什么? How can I "really" use the same CultureInfo /formatting everywhere?我怎样才能“真正”在任何地方使用相同的CultureInfo /formatting?

The CultureInfo on Windows uses machine/user level locale information and does not have any information on its own. Windows 上的CultureInfo使用机器/用户级别的区域设置信息,并且它自己没有任何信息。 This information is updated relatively frequently by OS versions and updates.操作系统版本和更新会相对频繁地更新此信息。 As result machines with different levels of patches or different versions of OS will likely have differences, usually minor as you see but sometime critical if something like decimal separator changes between '.'因此,具有不同级别的补丁或不同版本的操作系统的机器可能会有差异,通常如您所见,但如果小数分隔符在“。”之间发生变化,有时会很关键。 and ',' or currency symbol for a region changes.和 ',' 或地区的货币符号会发生变化。

Usually it is not a problem as such formatting is used only to show/parse values from the user and not to store data anywhere (which uses InvariantCulture ).通常这不是问题,因为这种格式仅用于显示/解析来自用户的值,而不是在任何地方存储数据(使用InvariantCulture )。 Most individual users would not ever use enough machines to notice difference in desktop apps.大多数个人用户永远不会使用足够多的机器来注意到桌面应用程序的差异。 And for server side code (ie ASP.Net) running multiple servers with different versions of OS is even less likely.而对于运行具有不同操作系统版本的多个服务器的服务器端代码(即 ASP.Net)来说,这种可能性就更小了。

If you really must show identical formats independent of OS you would have to build your own CultureInfo objects for locales you interested in. In many cases you can just create your own CultureInfo based on existing one and just patch properties you care about.如果您确实必须显示独立于操作系统的相同格式,则必须为您感兴趣的语言环境构建自己的 CultureInfo 对象。在许多情况下,您可以基于现有的 CultureInfo 创建自己的CultureInfo并修补您关心的属性。

暂无
暂无

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

相关问题 为什么CultureInfo.GetCultures(CultureTypes.SpecificCultures)在不同的计算机上返回不同的文化集 - Why does CultureInfo.GetCultures(CultureTypes.SpecificCultures) return different sets of cultures on different computers 为什么不同计算机或操作系统上的相同文化的日期格式不同? - Why is the date format different for the same culture on different computers or OS? WCF在不同的计算机之间工作,但不在同一台计算机上 - WCF Works between different computers, but not on same computer 将文件保存到不同计算机上的同一目录吗? - Saving a File To The Same Directory On Different Computers? 当 CultureInfo 设置为其他语言时,字符串未被识别为有效日期时间 - string was not recognized as valid date time when CultureInfo is set to a different language 当相同的应用程序(来自2台不同的计算机)试图写入xml文件时(位于服务器中) - Locking a xml file(located in a server) when the same application(from 2 different computers) is trying to write into it 比较 DateTime 时,FirstOrDefault 在不同的计算机上给出不同的结果? 对象 - FirstOrDefault giving different results on different computers when comparing DateTime? objects 使用CultureInfo格式化C#图表轴 - Formatting C# Chart Axis Using CultureInfo Thread.CurrentThread.CurrentUICulture为Android中相同设置的同一设备返回不同的CultureInfo - Thread.CurrentThread.CurrentUICulture returns different CultureInfo for the same device for the same settings in Android 在不同计算机上的应用速度 - Application speed in different computers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM