【问题标题】:Number Format Current Culture数字格式 当前文化
【发布时间】:2016-11-21 21:59:19
【问题描述】:

我试图获取数字格式以便使用当前文化从字符串转换为十进制,我的系统中有下一个配置:

我想使用当前的文化信息,但使用自定义格式,如小数符号、数字分组符号等。

有没有办法恢复自定义格式配置?

【问题讨论】:

    标签: c# internationalization format


    【解决方案1】:

    Windows 允许用户覆盖 CultureInfo 对象及其关联对象通过使用区域和 控制面板中的语言选项。由返回的 CultureInfo 对象 CurrentCulture 属性在 以下情况:

    • 如果当前线程区域性由 Windows 隐式设置 GetUserDefaultLocaleName 函数。

    • 如果当前线程文化 由 DefaultThreadCurrentCulture 属性定义的对应于 当前的 Windows 系统文化。

    • 如果当前线程文化是 明确设置为 CreateSpecificCulture 返回的文化 方法,并且该文化对应于当前的Windows系统 文化。
    • 如果当前线程区域性显式设置为区域性 由 CultureInfo(String) 构造函数实例化,并且该区域性 对应于当前的 Windows 系统文化。

    当您传递 CurrentCulture 时,它​​将用于将字符串传递给小数。

    decimal number;
    if (!decimal.TryParse(numberString, NumberStyles.Number, CultureInfo.CurrentCulture, out number))
    {
        // Something wrong...
    }
    

    【讨论】:

      【解决方案2】:

      您可以从CurrentThread 获取当前的文化编号格式。

      var format = Thread.CurrentThread.CurrentCulture.NumberFormat;
      

      编辑:

      CultureInfo.NumberFormat

      用户可能会选择覆盖一些与 通过区域和语言了解当前 Windows 文化 控制面板的选项部分。例如,用户可能会选择 以不同的格式显示日期或使用其他货币 比文化的默认值。

      如果 UseUserOverride 为 true 并且 指定的区域性与 Windows 的当前区域性匹配,则 CultureInfo 使用这些覆盖,包括 由返回的 DateTimeFormatInfo 实例的属性 DateTimeFormat 属性,以及 NumberFormatInfo 的属性 NumberFormat 属性返回的实例。如果用户设置 与与 CultureInfo 关联的文化不兼容,因为 例如,如果所选日历不是 OptionalCalendars 之一, 方法的结果和属性的值是 未定义。

      所以var decimalDigits = format.NumberDecimalDigits; 应该返回您的自定义更改。

      【讨论】:

      • 关闭,但问题是恢复当前文化的自定义格式。
      • @GonzaloAguayo 检查编辑。检查 UseUserOverride 是否为真。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      相关资源
      最近更新 更多