Powershell Variables and Floating point RRS feed

  • General discussion

  • I have been trying to find a way to store a floating point value in a variable in powershell.  Research sends me to several rounding and data types but I still cant get what I am trying for.  I have a script where the output comes at me like this on the screen...

    Get-Cluster -Name MPTC-VMC003 | Get-VM | Get-HardDisk | ?{$_.DiskType -eq "RawPhysical"}  | select Parent,Name,CapacityKB,@{name='CapacityMB';expression={$_.CapacityKB / 1024}},CapacityGB,DiskType | ft -AutoSize

    Parent            Name         CapacityKB       CapacityMB                CapacityGB    DiskType
    ------            ----         ----------       ----------                ----------    --------

    VirtualMachineName      Hard disk 4  1101004800          1075200                      1050 RawPhysical
    VirtualMachineName      Hard disk 5  1048576000          1024000                      1000 RawPhysical
    VirtualMachineName      Hard disk 6  1048576000          1024000                      1000 RawPhysical
    VirtualMachineName      Hard disk 7  1048576000          1024000                      1000 RawPhysical
    VirtualMachineName      Hard disk 8  1153433600          1126400                      1100 RawPhysical
    VirtualMachineName      Hard disk 9  1073741824          1048576                      1024 RawPhysical
    VirtualMachineName      Hard disk 10  524288000           512000                       500 RawPhysical
    VirtualMachineName      Hard disk 11 1073741824          1048576                      1024 RawPhysical
    VirtualMachineName      Hard disk 12 3221225472          3145728                      3072 RawPhysical
    VirtualMachineName      Hard disk 13 1288490188 1258291.19921875   1228.799999237060546875 RawPhysical
    VirtualMachineName      Hard disk 14 2684354560          2621440                      2560 RawPhysical
    VirtualMachineName      Hard disk 15 1503238553 1468006.39941406 1433.59999942779541015625 RawPhysical

    the calculated field comes out in a long decimal place as above like 1258291.19921875 however it gets truncated to 1258291.199 when i export with export-csv.

    Playing with variable assinment, I get strange results when trying to figure out how to store this.  I will assign a multidecimal place value to a variable, but get back a truncated value, or an exponent.

    $varvalue = 123213452345.23325235
    [double]$varvalue = 123213452345.23325235
    [float]$varvalue = 123213452345.23325235

    Ultimately I want to take the Powershell command at the top, export to csv the entire floating point value, not the truncated version of it.  

    • Changed type Bill_Stewart Friday, July 7, 2017 7:07 PM
    • Moved by Bill_Stewart Friday, July 7, 2017 7:07 PM User answered own question
    Tuesday, June 6, 2017 9:56 PM

All replies

  • I can't reproduce.

    PS C:\> [PSCustomObject] @{Test = 1258291.19921875} | Export-Csv Test.csv -NoTypeInformation
    PS C:\> Get-Content Test.csv

    Make sure you don't use Format-Table (ft) before exporting to CSV.

    Also note that CSV is text; if you know a column is floating-point, you will need to cast as such before importing it somewhere else.

    -- Bill Stewart [Bill_Stewart]

    Tuesday, June 6, 2017 10:13 PM
  • PS D:\scripts> $floater = 1.1
    PS D:\scripts> $floater
    PS D:\scripts> $floater.GetType()

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Double                                   System.ValueType


    PS D:\scripts> [double]$floater=0
    PS D:\scripts> $floater.GetType()

    IsPublic IsSerial Name                                     BaseType
    -------- -------- ----                                     --------
    True     True     Double                                   System.ValueType


    PS D:\scripts> '{0:N2}' -f ($varValue/1Gb)
    PS D:\scripts> $varvalue = 123213452345.23325235
    PS D:\scripts> $varValue/1Gb
    PS D:\scripts> '{0:N2}' -f ($varValue/1Gb)


    Tuesday, June 6, 2017 10:46 PM
  • Ok...this gets even more crazy...

    PS C:\Folder> [PSCustomObject] @{Test = 1258291.19921875} | Export-Csv Test.csv -NoTypeInformation PS C:\Folder> cat .\Test.csv "Test" "1258291.19921875" PS C:\Folder> $Test = 23453245245.1234567890 PS C:\Folder> $Test 23453245245.1235 PS C:\Folder> $PSVersionTable.PSVersion Major Minor Build Revision ----- ----- ----- -------- 4 0 -1 -1 PS C:\Folder>

    PS C:\Folder> $Test = 1234567890.1234567890
    PS C:\Folder> $Test
    PS C:\Folder> $Test = 123.1234567890
    PS C:\Folder> $Test
    PS C:\Folder> $Test = 1.1234567890
    PS C:\Folder> $Test
    PS C:\Folder>

    it appears that the number of integers in the number also affects the number of places stored in [double].  That is affecting my stored value it seems.  

    Tuesday, June 6, 2017 10:48 PM
  • By default you are getting a double which truncates.  The console interprets in that fashion.  This won't:

    PS D:\scripts> $test = [float]1234567890.1234567890
    PS D:\scripts> $test


    PS D:\scripts> [single]$test=1234567890.1234567890
    PS D:\scripts> $test
    PS D:\scripts> [float]$test=1234567890.1234567890
    PS D:\scripts> $test
    PS D:\scripts>


    Tuesday, June 6, 2017 11:01 PM
  • thanks everyone for the tips...ultimately, i had a mix of odd things going on.  I did some get-member'ing on the object that came from the vmware powercli was being passed as a Int64..

       TypeName: Selected.VMware.VimAutomation.ViCore.Impl.V1.VirtualDevice.RawHardDiskImpl

    Name        MemberType   Definition
    ----        ----------   ----------
    Equals      Method       bool Equals(System.Object obj)
    GetHashCode Method       int GetHashCode()
    GetType     Method       type GetType()
    ToString    Method       string ToString()
    CapacityKB  NoteProperty System.Int64 CapacityKB=1073741824

    when it got exported it was being converted to a double when the value was a decimal reply, but it was staying Int64 when the reply was coming up .0000000 in the division.  This was messing with excel and rounding or the export.

    by chaning the command line way up at top from @{name='CapacityMB';expression={$_.CapacityKB / 1024}} to @{name='CapacityMB';expression={[double]$_.CapacityKB / 1024}} it change all values to a double as it ran.  

    On top of that excel was also truncating, but when I set the format of the cell to be 8 decimal places out, the correct number now shows when using the [double] type, when leaving left to chance it was rounding the value a bit.

    Tuesday, June 6, 2017 11:35 PM