PowerShell,格式化另一种文化中的值 [英] PowerShell, formatting values in another culture

查看:23
本文介绍了PowerShell,格式化另一种文化中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 中是否有一种简单的方法可以在其他语言环境中格式化数字等?我目前正在编写一些函数来为我简化 SVG 生成,SVG 使用 . 作为小数分隔符,而 PowerShell 在转换时尊重我的语言环境设置 (de-DE)浮点数到字符串.

Is there an easy way in PowerShell to format numbers and the like in another locale? I'm currently writing a few functions to ease SVG generation for me and SVG uses . as a decimal separator, while PowerShell honors my locale settings (de-DE) when converting floating-point numbers to strings.

是否有一种简单的方法可以在不粘连的情况下为某个功能设置另一个区域设置

Is there an easy way to set another locale for a function or so without sticking

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

在每个 double 变量之后?

注意:这是关于用于格式化的locale不是格式字符串.

Note: This is about the locale used for formatting, not the format string.

(附带问题:在这种情况下我应该使用不变区域性还是en-US?)

(Side question: Should I use the invariant culture in that case or rather en-US?)

预计到达时间:好吧,我在这里尝试的是以下内容:

ETA: Well, what I'm trying here is something like the following:

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))
    )
}

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

Just a little automation for stuff I tend to write all the time and the double values need to use the decimal point instead of a comma (which they use in my locale).

ETA2:要添加的有趣琐事:

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

通过将变量放入字符串中,设置的语言环境似乎不适用,不知何故.

By putting the variable into a string the set locale doesn't seem to apply, somehow.

推荐答案

这是一个 PowerShell 函数,我用于在其他文化中测试脚本.我相信它可以用于您所追求的:

This is a PowerShell function I use for testing script in other cultures. I believe it could be used for what you are after:

function Using-Culture ([System.Globalization.CultureInfo]$culture =(throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"),
                        [ScriptBlock]$script=(throw "USAGE: Using-Culture -Culture culture -Script {scriptblock}"))
{    
    $OldCulture = [System.Threading.Thread]::CurrentThread.CurrentCulture
    $OldUICulture = [System.Threading.Thread]::CurrentThread.CurrentUICulture
    try {
        [System.Threading.Thread]::CurrentThread.CurrentCulture = $culture
        [System.Threading.Thread]::CurrentThread.CurrentUICulture = $culture        
        Invoke-Command $script    
    }    
    finally {        
        [System.Threading.Thread]::CurrentThread.CurrentCulture = $OldCulture        
        [System.Threading.Thread]::CurrentThread.CurrentUICulture = $OldUICulture    
    }    
}

PS> $res = Using-Culture fr-FR { 1.1 }
PS> $res
1.1

这篇关于PowerShell,格式化另一种文化中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆