zoukankan      html  css  js  c++  java
  • Powershell&.NET数值取整处理

    如何取一个数的整数值?

     

    • 使用类型强制转换

    Powershell的强制转换有2种方式,一种是直接类型强制转换,另一种是通过-as运算符进行转换

    1. PS F:> [int] (3 / 2) # 直接类型强制转换 
    2. PS F:> (3 / 2) -as [int] # -as运算符进行类型转换 
    3. PS F:> 

    强制转换的结果是四舍五入计算的。

     

    • 使用,NET

    现在我们需要找另一种方法来解决问题:取【不大于值】的【最大整数】

    用【.NET类】中【Math类】的【Floor方法】可以实现

    1. PS F:> [math]::floor(3 / 2) 

    但这种方法只对正数有效。如果是负数,就要用[math]::ceiling了,取不小于参数值的最小整数。

    1. PS F:> [math]::Ceiling(-3/2)
    2. -1

     

    $size=("{0:N2}" -f ($subFolderItems.sum / 1GB))         保留小数点后两位

    $size=[math]::truncate($disk.size/1GB)                            截取小数点,保留整数

     

     

    $size=("{0:N2}" -f ($subFolderItems.sum / 1GB))         保留小数点后两位

    $size=[math]::truncate($disk.size/1GB)                            截取小数点,保留整数

  • 相关阅读:
    topcoder srm 320 div1
    topcoder srm 325 div1
    topcoder srm 330 div1
    topcoder srm 335 div1
    topcoder srm 340 div1
    topcoder srm 300 div1
    topcoder srm 305 div1
    topcoder srm 310 div1
    topcoder srm 315 div1
    如何统计iOS产品不同渠道的下载量?
  • 原文地址:https://www.cnblogs.com/thescentedpath/p/PowershellNET.html
Copyright © 2011-2022 走看看