简体   繁体   English

如何使用CultureInfo在C#中获取和设置小数点分隔符

[英]How to get and set decimal separator in C# using CultureInfo

I'm converting a routine from VB6 (a language I do not know) to C#. 我正在将例程从VB6(一种我不知道的语言)转换为C#。 In the VB6 code it uses GetLocaleInfo() and SetLocaleInfo() and I posted a question to ask how to do this in C# and I was told to use "CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator" instead. 在VB6代码中,它使用GetLocaleInfo()SetLocaleInfo(),然后我发布了一个问题 ,询问如何在C#中执行此操作,并被告知要使用“ CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator”代替。

For my purposes I'm only interested in getting the decimal separator and setting it for whatever my current locale is. 就我的目的而言,我只对获取小数点分隔符并将其设置为当前的语言环境感兴趣。 Following the links in the answers to the original question it wasn't obvious how to do this. 按照原始问题的答案中的链接,如何执行此操作并不明显。

In the original code the programmer uses the American "." 在原始代码中,程序员使用美国的“”。 format internally for lots of string formatting and building so he first gets the current locale's separator (say, ",") and saves it, then he sets the current locale to use "." 内部格式化许多字符串格式和构建,因此他首先获取当前语言环境的分隔符(例如,“,”)并保存,然后将当前语言环境设置为使用“。” and does all his stuff, then he sets it back to what he saved when he finishes. 并完成所有工作,然后他将其恢复为完成时保存的内容。 My marching orders are to stick as closely as possible to the original design, which is why I asked about GetLocaleInfo() and SetLocaleInfo() , but if someone could show how to do this CultureInfo I'd appreciate it. 我的前进顺序是尽可能地与原始设计保持一致,这就是为什么我询问GetLocaleInfo()SetLocaleInfo()的原因 ,但是如果有人可以演示如何执行此CultureInfo,我将不胜感激。

EDIT: I don't understand what people don't like about this question. 编辑:我不明白人们不喜欢这个问题。 I also don't understand what code people are looking for. 我也不明白人们在寻找什么代码。 This is all the code there is in the VB6 这就是VB6中的所有代码

 LCID = GetThreadLocale 
 rc = SetLocaleInfo(LCID, LOCALE_SDECIMAL, ".")

That is the code. 就是代码。 I want to do the same thing in C#. 我想在C#中做同样的事情。 My understanding from our VB6 programmer is that it gets the current locale setting (say, German) and says regardless of the fact that German uses "," as a decimal separator, we are going to use "." 我对VB6程序员的理解是,它将获得当前的语言环境设置(例如德语),并说不管德语使用“,”作为小数点分隔符,我们都将使用“”。 .

You appear to be looking for the "NumberDecimalSeparator" property of the "NumberFormatInfo" object (System.Globalization), as you mentioned. 如前所述,您似乎正在寻找“ NumberFormatInfo”对象(System.Globalization)的“ NumberDecimalSeparator”属性。 Here's the msdn article on it. 这是有关它的msdn文章。 The usage (from the msdn page) is as follows: 用法(来自msdn页面)如下:

// Gets a NumberFormatInfo associated with the en-US culture.
  NumberFormatInfo nfi = new CultureInfo( "en-US", false ).NumberFormat;

  // Displays a value with the default separator (".").
  Int64 myInt = 123456789;
  Console.WriteLine( myInt.ToString( "N", nfi ) );

  // Displays the same value with a blank as the separator.
  nfi.NumberDecimalSeparator = " ";
  Console.WriteLine( myInt.ToString( "N", nfi ) );

You can change the separator with the NumberDecimalSeparator = "" line as seen above. 您可以使用NumberDecimalSeparator =“”行更改分隔符,如上所示。 All of your numeric outputs will use the separator as specified. 您所有的数字输出都将使用指定的分隔符。

Likewise, you can use the NumberGroupSeparator property to handle the separation of whole number digits: 同样,可以使用NumberGroupSeparator属性处理整数位数的分隔:

nfi.NumberGroupSeparator = ".";

...will make "123,456,789" display as "123.456.789", etc. ...将使“ 123,456,789”显示为“ 123.456.789”,依此类推。

Hopefully that is a more clear and concise explanation than the one you linked. 希望这是一个比您所链接的解释更加清晰和简洁的解释。

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

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