Cmdlet to round output numbers

Is there a cmdlet that rounds numbers (all float values) in the output?

When I run the following command:

get-vm |select ProvisionedSpaceGB,UsedSpaceGB

I get this output as a table:

            ProvisionedSpaceGB                    UsedSpaceGB
            ------------------                    -----------
1224,0003194380551576614379883 349,88938544876873493194580078
1224,0003062393516302108764648 321,74483488313853740692138672
502,80292716529220342636108399 74,052481059916317462921142578
700,00035238638520240783691406 484,56624550372362136840820312
 800,0003144945949316024780273 322,26342210918664932250976562

I know that I can determine what is in the cmdlet selectlike this:

 get-vm | select @{ n="ProvisionedSpaceGB"; e={[math]::round( $_.ProvisionedSpaceGB, 2 )}},
    @{ n="UsedSpaceGB"; e={[math]::round( $_.UsedSpaceGB, 2 )}}

And then get this output:

ProvisionedSpaceGB UsedSpaceGB
------------------ -----------
           1224,00      349,58
           1224,00      320,32
            502,80       74,05
            700,00      484,57
            800,00      322,26

But there should be an easier way, for example, linking it to another cmdlet:

get-vm |select ProvisionedSpaceGB,UsedSpaceGB |RoundNumbers -Precision 2

To get the second conclusion.

+4
source share
1 answer

I doubt there is a built-in PowerShell cmdlet for this, but you can write your own filter:

filter Round-FloatValues
{
    Param(
        [parameter(ValueFromPipeline=$true)]
        $objects,

        [int]$Precision = 2        
    )

    $objects.PsObject.Properties | ForEach-Object {
        if ($_.TypeNameOfValue -eq 'System.Double')
        {
            [math]::round( $_.Value, $Precision )
        }
        else
        {
            $_.Value
        }
    }
}

Application:

get-vm |select ProvisionedSpaceGB,UsedSpaceGB |Round-FloatValues -Precision 2
+5
source

Source: https://habr.com/ru/post/1661919/


All Articles