Automation

 View Only
  • 1.  Rounding numbers

    Posted Dec 10, 2008 12:10 AM

    This is more of a generic powershell question than a VI Toolkit question, but I have been googling like crazy and haven't been able to find an answer yet. When using the ::round() function, is it possible to always round the result up? So like if it's 3.1, I want the result ot be 4. Thanks.



  • 2.  RE: Rounding numbers

    Posted Dec 10, 2008 12:48 AM

    ::Round() has some various overloads (sets of parameters), but none of them will do what you are looking for. Here's a way to do it:

    ( [math]::Truncate( $int ) ) +1






    [PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator

    Author of the upcoming book: Managing VMware Infrastructure with PowerShell

    Co-Host, PowerScripting Podcast (http://powerscripting.net)

    Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org



  • 3.  RE: Rounding numbers
    Best Answer

    Posted Dec 10, 2008 01:28 AM

    Well, that's one way to go too.

    I was going to suggest math::ceiling. math::floor goes the other way.

    Please note that you have to add square brackets around math in both of the above.



  • 4.  RE: Rounding numbers

    Posted Dec 10, 2008 01:45 AM

    Sweet! Thanks for your replies guys. math::ceiling($int) works best for me because sometimes the $int is already a whole number, so I don't want to add 1 to it.