对[math] :: round的相同输入将返回不同的结果 [英] Same input into [math]::round returns different results

查看:37
本文介绍了对[math] :: round的相同输入将返回不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PowerShell中解析文件.我有这段代码(带有调试写)

I'm parsing a file in PowerShell. I have this section of code (with debug writes)

$max_file_size = 0
$max_file_size = $value
Write-Host $max_file_size # DEBUG
$max_file_size = Convert-to-Bytes $max_file_size
Write-Host $max_file_size # DEBUG
[string]$max_file_size = [math]::round($max_file_size, 0)
Write-Host $max_file_size # DEBUG

我看到两条不同的行具有相同的数据.第一遍返回此结果:

I'm seeing two different lines with the same data. The first pass returns this result:

8192.00000P
9223372036854775808.00000
9223372036854775808

第二遍返回此结果:

8192.00000P
9223372036854775808.00000
9.22337203685478E+18

Wat?

Function Convert-to-Bytes
{
    #Pass in a value and convert to bytes.
    param ($value)

    If ($value -eq $Null)
    {
        $value = 0
    }

    #Convert case to upper.
    [string]$value = $value.ToString().ToUpper()

    #Strip off 'B' if in string.
    [string]$value = $value.TrimEnd("B")

    [decimal]$output = 0

    If ($value.Contains("K"))
    {
        $value = $value.TrimEnd("K")
        [decimal]$output = $value
        [decimal]$output = $output * 1KB
    }
    If ($value.Contains("M"))
    {
        $value = $value.TrimEnd("M")
        [decimal]$output = $value
        [decimal]$output = $output * 1MB
    }
    If ($value.Contains("G"))
    {
        $value = $value.TrimEnd("G")
        [decimal]$output = $value
        [decimal]$output = $output * 1GB
    }
    If ($value.Contains("T"))
    {
        $value = $value.TrimEnd("T")
        [decimal]$output = $value
        [decimal]$output = $output * 1TB
    }
    If ($value.Contains("P"))
    {
        $value = $value.TrimEnd("P")
        [decimal]$output = $value
        [decimal]$output = $output * 1PB
    }
    If ($value.Contains("yte"))
    {
        $value = $value.TrimEnd("yte")
        [decimal]$output = $value
        [decimal]$output = $output
    }
    Return $output
}

推荐答案

声明类型变量时,PowerShell向其添加 ArgumentTypeConverterAttribute ,因此对该变量的所有后续赋值都将转换为该类型.

When you declare typed variable, then PowerShell add ArgumentTypeConverterAttribute to it, so all subsequent assignment to that variable would be converted to this type.

PS> $Var1=$null
PS> [string]$Var2=$null
PS> Get-Variable Var?|ft Name,Attributes -AutoSize

Name Attributes
---- ----------
Var1 {}
Var2 {System.Management.Automation.ArgumentTypeConverterAttribute}


PS> $Var1=1
PS> $Var2=1
PS> $Var1.GetType().FullName
System.Int32
PS> $Var2.GetType().FullName
System.String

在第一遍结束时,您声明 max_file_size 输入为 string :

At the end of first pass you declare max_file_size typed as string:

[string]$max_file_size = [math]::round($max_file_size, 0)

因此,第二遍通过时,您分配给它的任何内容都会转换为 string .

So on the second pass anything you assign to it got converted to string.

这篇关于对[math] :: round的相同输入将返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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