PowerShell,用另一种文化格式化值

2024-04-17

PowerShell 中有没有一种简单的方法可以在另一个语言环境中格式化数字等?我目前正在编写一些函数来简化我的 SVG 生成和 SVG 使用.作为小数点分隔符,而 PowerShell 遵循我的区域设置(de-DE) 将浮点数转换为字符串时。

有没有一种简单的方法可以为某个函数设置另一个区域设置而不粘住

.ToString((New-Object Globalization.CultureInfo ""))

每次之后double多变的?

注:这是关于locale用于格式化,not格式字符串。

(附带问题:在这种情况下我应该使用不变的文化还是更确切地说en-US?)

ETA:好吧,我在这里尝试的是类似以下内容:

function New-SvgWave([int]$HalfWaves, [double]$Amplitude, [switch]$Upwards) {
    "<path d='M0,0q0.5,{0} 1,0{1}v1q-0.5,{2} -1,0{3}z'/>" -f (
        $(if ($Upwards) {-$Amplitude} else {$Amplitude}),
        ("t1,0" * ($HalfWaves - 1)),
        $(if ($Upwards -xor ($HalfWaves % 2 -eq 0)) {-$Amplitude} else {$Amplitude}),
        ("t-1,0" * ($HalfWaves - 1))
    )
}

只是对我经常写的东西进行一点自动化,双精度值需要使用小数点而不是逗号(他们在我的语言环境中使用逗号)。

ETA2:有趣的琐事补充:

PS Home:> $d=1.23
PS Home:> $d
1,23
PS Home:> "$d"
1.23

通过将变量放入字符串中,设置的区域设置似乎不适用,不知何故。


While 基思·希尔的有用回答 https://stackoverflow.com/a/2380257/45375向您展示如何按需更改脚本的当前区域性(从 PSv3+ 和 .NET Framework v4.6+ 开始,更现代的替代方案:
[cultureinfo]::CurrentCulture = [cultureinfo]::InvariantCulture), 有no需要改变文化,因为 - 正如您在对问题的第二次更新中发现的 -PowerShell 的字符串插值法- 而不是使用-f运算符 - 始终使用不变的而不是current culture:

换句话说:

如果你更换'val: {0}' -f 1.2 with "val: $(1.2)", 数字字面量1.2 is not根据当前文化的规则进行格式化。
您可以通过运行 (在一条线上; PSv3+、.NET 框架 v4.6+):

 PS> [cultureinfo]::currentculture = 'de-DE'; 'val: {0}' -f 1.2; "val: $(1.2)"
 val: 1,2 # -f operator: GERMAN culture applies, where ',' is the decimal mark
 val: 1.2 # string interpolation: INVARIANT culture applies, where '.' is the decimal mark.

注:在PowerShell(核心)7+,对不同文化的改变在会议的剩余时间内仍然有效(正如它应该为Windows PowerShell也是,但没有)。


背景:

By design,[1] but perhaps surprisingly, PowerShell applies the invariant rather than the current culture in the following string-related contexts, if the type at hand supports culture-specific conversion to and from strings:

正如中所解释的这个深入的答案 https://stackoverflow.com/a/30542422/45375, PowerShell 明确请求文化不变加工,如果可能的话- 通过通过[cultureinfo]::InvariantCulture https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.invariantculture实例 - 在以下场景中(PowerShell 执行的字符串化相当于调用.psobject.ToString([NullString]::Value, [cultureinfo]::InvariantCulture)值):

  • When 字符串插值:如果对象的类型实现了IFormattable界面。

  • When casting:

    • to一个字符串, 包括implicit转换 when 绑定到一个[string]- 类型化参数:如果源类型实现了[IFormattable] https://learn.microsoft.com/en-us/dotnet/api/system.iformattable界面。

    • from一个字符串:如果目标类型是静态的.Parse()方法有一个重载[IFormatProvider] https://learn.microsoft.com/en-US/dotnet/api/System.IFormatProvider- 类型化参数(这是一个由[cultureinfo] https://learn.microsoft.com/en-US/dotnet/api/System.Globalization.CultureInfo).

  • When 字符串比较 (-eq, -lt, -gt) , 用一个String.Compare()超载 https://learn.microsoft.com/en-us/dotnet/api/system.string.compare接受一个CultureInfo范围。

  • Others?

请注意,单独地,custom字符串化应用于以下 .NET 类型的强制转换/隐式字符串化:

  • Arrays更一般地说,PowerShell 在管道中枚举的类似列表的集合类型(请参阅这个答案 https://stackoverflow.com/a/65530467/45375这些类型是什么)。

    • (字符串化)elements这些类型的连接spaces(严格来说:用很少使用的字符串中指定的$OFS偏好变量 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Preference_Variables#ofs);元素的字符串化递归地遵循此处描述的规则。

    • E.g, [string] (1, 2) yields '1 2'

  • [pscustomobject]

    • 这样的实例会产生一个哈希表 -like中描述的字符串格式这个答案 https://stackoverflow.com/a/53107600/45375; e.g.:

      # -> '@{foo=1; bar=2.2}'; values are formatted with the *invariant* culture
      [string] ([pscustomobject] @{ foo = 1; bar = 2.2 })
      
    • 事实是调用.ToString()直接在一个[pscustomobject]实例确实not产生这个表示并返回空字符串应该被视为bug - see GitHub 问题 #6163 https://github.com/PowerShell/PowerShell/issues/6163.

  • Others?

As for 的目的不变的文化 https://learn.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo.invariantculture:

不变文化是文化不敏感的;这是与英语相关,但与任何国家/地区无关.
[...]
与文化敏感数据不同,文化敏感数据可能会因用户自定义或 .NET Framework 或操作系统的更新而发生更改,而不变的文化数据是随着时间的推移和跨文化的稳定并且不能由用户定制。这使得不变区域性对于需要区域性独立结果的操作特别有用,例如保留格式化数据的格式化和解析操作,或者要求数据以固定顺序显示而不管区域性的排序和排序操作。

据推测,正是跨文化的稳定性促使 PowerShell 的设计者在以下情况下始终如一地使用不变的文化:隐含地与字符串之间的转换.

例如,如果您硬编码日期字符串,例如'7/21/2017'到一个脚本中,然后尝试将其转换为日期[date]强制转换,PowerShell 的文化不变行为可确保脚本在美国英语以外的文化生效时运行时不会中断 -幸运的是,不变文化还可以识别 ISO 8601 格式的日期和时间字符串;
e.g., [datetime] '2017-07-21'也有效。

另一方面,如果您确实想转换为current-适合文化的字符串,你必须这样做明确地.

总结一下:

  • 转换to strings:

    • 在内部嵌入具有文化敏感的默认字符串表示形式的数据类型实例"..."产生一种文化——不变的代表([double] or [datetime]是此类类型的示例)。
    • 为了得到一个current-文化代表、召唤.ToString()明确或使用-f https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#format-operator--f),格式化运算符(可能在内部"..."通过一个封闭的$(...)).
  • 转换from strings:

    • 直接演员表([<type>] ...)只承认文化-不变的字符串表示。

    • 要从current-适合文化的字符串表示形式(或specific文化的表示),使用目标类型的静态::Parse()显式方法(可选地带有明确的[cultureinfo]代表特定文化的实例)。


文化不变的例子:

  • 字符串插值 and casts:

    • "$(1/10)" and [string] 1/10

      • 两者都会产生字符串文字0.1, 带小数点.,无论当前的文化如何。
    • 相似地,casts from strings具有文化不变性;例如。,[double] '1.2'

      • . is always被识别为小数点,无论当前的文化如何。
      • 另一种说法是:[double] 1.2 is not翻译成文化——敏感的- 默认方法重载[double]::Parse('1.2'),但对于文化来说——不变的 [double]::Parse('1.2', [cultureinfo]::InvariantCulture)
  • 字符串比较(假使,假设[cultureinfo]::CurrentCulture='tr-TR'有效 - 土耳其语,其中i不是小写表示I http://mattryall.net/blog/2009/02/the-infamous-turkish-locale-bug)

    • [string]::Equals('i', 'I', 'CurrentCultureIgnoreCase')
      • $false土耳其文化的影响。
      • 'i'.ToUpper()表明在土耳其文化中大写是İ, not I.
    • 'i' -eq 'I'
      • 还是$true,因为不变的文化被应用。
      • 隐式地等同于:[string]::Equals('i', 'I', 'InvariantCultureIgnoreCase')

文化敏感的例子:

在以下情况下,当前文化受到尊重:

  • With -f,字符串格式化运算符(如上所述):

    • [cultureinfo]::currentculture = 'de-DE'; '{0}' -f 1.2 yields 1,2
    • Pitfall: Due to operator precedence https://msdn.microsoft.com/powershell/reference/5.1/Microsoft.PowerShell.Core/about/about_Operator_Precedence, any expression as the RHS of -f must be enclosed in (...) in order to be recognized as such:
      • E.g., '{0}' -f 1/10被评估为好像('{0}' -f 1) / 10已被指定;
        use '{0}' -f (1/10)反而。
  • Default输出到控制台:

    • e.g., [cultureinfo]::CurrentCulture = 'de-DE'; 1.2 yields 1,2

    • 这同样适用于 cmdlet 的输出;例如。,
      [cultureinfo]::CurrentCulture = 'de-DE'; Get-Date '2017-01-01' yields
      Sonntag, 1. Januar 2017 00:00:00

    • Caveat:在某些情况下,作为不受约束的参数传递到脚本块的文字可能会导致区域性不变的默认输出 - 请参阅GitHub 问题 #4557 https://github.com/PowerShell/PowerShell/issues/4557 and GitHub 问题 #4558 https://github.com/PowerShell/PowerShell/issues/4558.

  • 在所有?)cmdlets:

    • 那些表现的平等比较:

      • Select-Object https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object-Unique转变;还请注意 - 异常 - 情况 -敏感的执行比较,并且从 PowerShell 7.2.4 开始,不区分大小写甚至不能作为选择加入 - 请参阅GitHub 问题 #12059 https://github.com/PowerShell/PowerShell/issues/12059.
      • Select-Object https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object
      • Compare-Object https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/compare-object
      • Others?
    • 那些写入文件:

      • Set-Content https://learn.microsoft.com/powershell/module/microsoft.powershell.management/set-content and Add-Content https://learn.microsoft.com/powershell/module/microsoft.powershell.management/add-content
      • Out-File https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/out-file and therefore its virtual alias, > (and >>)
        • e.g., [cultureinfo]::CurrentCulture = 'de-DE'; 1.2 > tmp.txt; Get-Content tmp.txt yields 1,2
  • 由于.NET的逻辑,当使用静态::Parse() / ::TryParse()数字类型的方法,例如[double] https://learn.microsoft.com/en-us/dotnet/api/system.double.parse?view=netframework-4.7.1#System_Double_Parse_System_String_仅传递要解析的字符串;例如,与文化fr-FR实际上(其中,是小数点),[double]::Parse('1,2')返回双倍1.2 (i.e., 1 + 2/10).

    • Caveat: As bviktor https://stackoverflow.com/users/1708230/bviktor points out, thousands separators are recognized by default, but in a very loose fashion: effectively, the thousands separator can be placed anywhere inside the integer portion, irrespective of how many digits are in the resulting groups, and a leading 0 is also accepted; e.g., in the en-US culture (where , is the thousands separator), [double]::Parse('0,18') perhaps surprisingly succeeds and yields 18.
      • 要抑制对千位分隔符的识别,请使用类似[double]::Parse('0,18', 'Float'),通过NumberStyles范围 https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberstyles?view=netframework-4.7.1
  • 无心的文化敏感性不会被纠正以保持向后兼容性:

    • 在编译的参数绑定类型转换中cmdlets (but PowerShell代码- 脚本或函数 -is文化不变) - 参见GitHub 问题 #6989 https://github.com/PowerShell/PowerShell/issues/6989.
    • In the -as操作员 - see GitHub 问题 #8129 https://github.com/PowerShell/PowerShell/issues/8129.
    • In [hashtable]关键查找 - see 这个答案 https://stackoverflow.com/a/53324566/45375 and GitHub 问题 #8280 https://github.com/PowerShell/PowerShell/issues/8280.
    • [在 v7.1+ 中修复] 在 LHS 中-replace运营 - see GitHub 问题 #10948 https://github.com/PowerShell/PowerShell/issues/10948.
  • Others?


[1] The aim is to support programmatic processing using representations that do not vary by culture and do not change over time. See the linked quote from the docs later in the answer.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PowerShell,用另一种文化格式化值 的相关文章

随机推荐

  • 对于案例,这些表达案例的方法中哪种最好?

    这些都有效 defun testcaseexpr thecase case thecase foo format t matched foo bar format t matched bar funk format t matched fu
  • 如何估算 BLE 模块的功耗?

    我正在为带有 BLE 模块的设备编写一个 iOS 应用程序 该模块在连接时一致地通告几个字节的数据 我们正在尝试估算 BLE 模块的功耗 以便估算设备的电池寿命 我已经在 SO 和 Google 上搜索过 寻找合适的方法来估计这一点 但我一
  • 如何判断一个对象是否具有给定的原型?

    如何检测给定浏览器是否具有searchParams原型为URL https developer mozilla org en US docs Web API URL searchParams https developer mozilla
  • 如何使用 python 检查带有命令行参数的进程是否正在运行

    我想检查脚本是否正在使用 python 脚本中的特定命令行参数运行 例如我想检查是否 main py testarg 在跑 我有什么办法可以实现这个目标吗 提前致谢 要搜索当前正在运行的进程 您应该使用诸如psutil http code
  • 在 Web Worker 中导入 Tensorflow 时出现 Angular TypeScript 类型检查问题

    我正在尝试在有角度的项目中的网络工作人员中使用tensorflow tfjs TF 使用以下命令创建网络工作者ng generate worker命令工作得很好 在组件中导入 TF 也很好 但是 在工作人员中导入 TF 即 import a
  • 为什么我收到“不建议在全局范围内包含 Capybara::DSL!”

    每次我运行规范时 即使规范通过了 例如 rspec spec integration view homepage spec rb including Capybara DSL in the global scope is not recom
  • 更改日期选择器的文本颜色

    有什么方法可以改变 iOS 8 中日期选择器的文本颜色吗 在 iOS 7 及更早版本中这是不可能的吗 在第 8 版本中发生了一些变化 例如 我在雅虎天气中找到了修改后的选择器 仍然有效 Swift 4 更新 datePicker setVa
  • 某些用户从 Facebook 获取“验证访问令牌时出错”

    当我尝试发布到用户流时 我从 Facebook 收到以下错误 Error validating access token The session has been invalidated because the user has chang
  • 如何在 JavaScript 中复制变量?

    我有这个 JavaScript 代码 for var idx in data var row tr tr row click function alert idx table append row 因此 我正在查看一个数组 动态创建行 创建
  • 如何在同一个列表上迭代多个资源?

    这里是 Terraform 的新手 我正在尝试使用 Terraform 创建多个项目 在 Google Cloud 中 问题是我必须执行多个资源才能完全建立一个项目 我试过count 但是如何使用顺序绑定多个资源count 以下是我需要为每
  • 从 data.frame 中的现有变量创建几个新的派生变量

    在 R 中 我有一个 data frame 其中有几个变量 这些变量是多年来每月测量的 我想得出每个变量的月平均值 使用所有年份 理想情况下 这些新变量将全部放在一个新的 data frame 中 继承 ID 下面我只是将新变量添加到 da
  • MultipartEntityBuilder 和 setCharset for UTF-8 发送空内容

    我需要将 unicode 字符提交到表单 以将我的应用程序本地化到使用非拉丁字母的国家 地区 关于新的 MultiPartEntityBuiler 的文档很少 我只找到了另一篇建议使用 setCharset 的帖子 如果我不使用 Entit
  • 从 mongoose 'toJSON' 支持中删除一个属性

    我正在使用 mongoose 的 toJSON 支持 如下所示 userSchema set toJSON getters true virtuals true minimize false 现在 在猫鼬对象的 toJSON 方法调用返回的
  • 使用 Office.js 在桌面 Excel 上呈现数据,但在 Chrome Office 365 上,它会给出错误“处理请求时出错”。

    我们正在使用办公js在 Excel 上呈现数据的库 有超过2000行效果很好桌面Excel 但当同样API用于Chrome 办公室 365它给出错误消息 有一个错误处理请求 请参阅随附的屏幕截图 随着数据变大 Chrome Office E
  • 如何获取hive中的数据库用户名和密码

    正在编写jdbc程序来连接hive数据库 我希望在连接 url 中提供用户名和密码 我不知道如何使用 hive QL 获取用户名和密码 有人可以帮我吗 Exception in thread main java sql SQLNonTran
  • 如何向 CPBarPlot 条形图添加标签?

    我对 Core Plot 完全陌生 并且有一个可用的条形图 但视觉效果对我来说有点无用 除非我知道每个条形中代表哪个对象 我看到有一个名为 fieldIdentifiers 的方法 但不知道如何实现它 也找不到任何文档 如果这甚至是正确的方
  • 禁用 ActionBar RTL 方向

    Android 4 2 引入了 RTL BiDi 支持 要开始使用它 我只需按照说明进行操作 清单文件中元素的 android supportsRtl 属性并将其设置为 true 但随后我的 ActionBar 徽标也将方向更改为右侧 徽标
  • 未使用 QueryString id 参数

    我有一个非常基本的 ASP Net MVC 项目 我想在我的控制器操作之一上使用 id 参数名称 从我读过的所有内容来看 这应该不是问题 但由于某种原因 使用 id 参数名称无法获取从查询字符串中提取的值 但如果我将其更改为任何其他不同的名
  • 获取第n行文本输出

    我有一个每次生成两行作为输出的脚本 我真的只对第二行感兴趣 此外 我只对第二行一对 之间出现的文本感兴趣 此外 在散列之间还使用另一个分隔符 A 如果我还可以分解以 A 分隔的文本的每个部分 那就太好了 请注意 A 是 SOH 特殊字符 可
  • PowerShell,用另一种文化格式化值

    PowerShell 中有没有一种简单的方法可以在另一个语言环境中格式化数字等 我目前正在编写一些函数来简化我的 SVG 生成和 SVG 使用 作为小数点分隔符 而 PowerShell 遵循我的区域设置 de DE 将浮点数转换为字符串时