powershell十进制[int]未失败

发布于 2025-01-28 11:15:53 字数 1806 浏览 3 评论 0原文

我正在尝试验证应将其用作成功代码的整数值的字符串。如果我从具有某些无效值的数组开始,那么……

$test = @(0, 100, -200, 3.3, 'string')
foreach ($item in $test) {
    if ($item -isnot [Int]) {
        Write-Host "![Int] $item"
    } else {
        Write-Host "[Int] $item"
    }
}

我得到了预期的结果,前三个是[int],而最后两个则没有。

但是,如果我从字符串开始并构建数组,

$testString = '0, 100, -200, 3.3, string'
$test = $testString.Split(',').Trim()

那么所有测试都不是[int],这是有道理的,因为数组将是字符串。我需要先施放[int]。但是当我这样做时,像

$testString = '0, 100, -200, 3.3, string'
$test = $testString.Split(',').Trim()
foreach ($item in $test) {
    if ($item -as [Int] -isnot [Int]) {
        Write-Host "![Int] $item"
    } else {
        Write-Host "[Int] $item"
    }
}

[int]一样,3.3返回。我发现 this ,这向我暗示我太聪明了,这对什么太聪明了我似乎正在尝试这样做。因此,是否有一种方法可以施放十进制INT和发生故障的小数,或者我需要两个条件,一个可以铸造和捕获真实的字符串,一种来寻找,从而找到小数?或者,也许要将[int]的值与[单个]这样的值进行比较?

$testString = '0, 100, -200, 3.3, string'
$test = $testString.Split(',').Trim()
foreach ($item in $test) {
    if ($item -as [Int32] -isnot [Int32]) {
        Write-Host "![Int] $item"
    } else {
        Write-Host "[Int] $item"
    }
}

这是我设法上班的最优雅的方式,但是我想知道是否有更好的方法? 我还尝试了如果([int] $ item -isnot [int]){,我认为是实际的铸件,而-as在技术上是胁迫未能捕获小数,同时在字符串上丢下错误。因此,我认为我比我更了解铸造与胁迫要比我更了解,并且上面的工作代码与之一样好。希望对两者进行一些验证或反驳。

编辑:实际上,这仅是因为我的小数不是.0。如果我想将3.0标记为错误,以及3.3,我真的需要像这样检查小数。

if (($item -as [Int] -isnot [Int]) -or ($item -like '*.*')) {

I am trying to validate a string of what should be comma separated integer values to use as success codes. If I start with an array with some invalid values, like this...

$test = @(0, 100, -200, 3.3, 'string')
foreach ($item in $test) {
    if ($item -isnot [Int]) {
        Write-Host "![Int] $item"
    } else {
        Write-Host "[Int] $item"
    }
}

I get the expected results, the first three are [Int] and the last two are not.

However, if I start from a string and build the array, like this

$testString = '0, 100, -200, 3.3, string'
$test = $testString.Split(',').Trim()

then all tests are not [Int], which makes sense because the array will be strings. I need to cast to [Int] first. But when I do, like this

$testString = '0, 100, -200, 3.3, string'
$test = $testString.Split(',').Trim()
foreach ($item in $test) {
    if ($item -as [Int] -isnot [Int]) {
        Write-Host "![Int] $item"
    } else {
        Write-Host "[Int] $item"
    }
}

3.3 is returning as [Int]. I found this which clues me in that casting is too smart for what I am trying to do it seems. So, IS there a way to cast a decimal to int and get a failure, or do I need two conditionals, one to cast and catch the true strings, and one to look for . and thus find the decimals? Or perhaps to compare the value of [Int] with a cast to [Single] like this?

$testString = '0, 100, -200, 3.3, string'
$test = $testString.Split(',').Trim()
foreach ($item in $test) {
    if ($item -as [Int32] -isnot [Int32]) {
        Write-Host "![Int] $item"
    } else {
        Write-Host "[Int] $item"
    }
}

This is the most elegant way I have managed to get to work, but I wonder if there is actually a better approach?
I also tried if ([Int]$item -isnot [Int]) {, which is I think an actual Cast, while -as is technically Coercion, and that still fails to catch the decimal, while throwing an error on the string. So I am left thinking I understand Casting vs Coercion better than I did, and the working code above is as good as it gets. Hoping for some verification or refutation on both.

EDIT: Actually, that only works because my decimal is not .0. If I want to flag 3.0 as an error as well as 3.3 I really need to check for the decimal instead, like so.

if (($item -as [Int] -isnot [Int]) -or ($item -like '*.*')) {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文