chop right of the decimal point

Discussion in 'Scripting' started by Rob Pettrey, Jul 27, 2006.

  1. Rob Pettrey

    Rob Pettrey Guest

    Hi everyone,

    I need a backup script solution, please. If the day is not Friday:

    $dow=(get-date).dayofweek.ToString()

    If it's Friday:

    if ($dow -eq "Friday")
    {
    $dom=(get-date).Day.ToString()
    $wom=$dom/7
    $label="Week "+$wom
    write-output $label
    }

    How can I make it 'Week 3' not "Week 3.714285714285...'?
     
    Rob Pettrey, Jul 27, 2006
    #1
    1. Advertisements

  2. Rob Pettrey

    Babu VT Guest

    Babu VT, Jul 27, 2006
    #2
    1. Advertisements

  3. Rob Pettrey

    Rob Pettrey Guest

     
    Rob Pettrey, Jul 27, 2006
    #3
  4. Rob Pettrey

    Rob Pettrey Guest

    How do I truncate, not round?

    With today's date (27), if I do:
    [int]$wom=$dom/7 or
    $wom=[int]$dom/7

    I get 4. I need to get 3.
     
    Rob Pettrey, Jul 27, 2006
    #4
  5. Rob Pettrey

    dreeschkind Guest

    [Math]::Truncate(3.1415926535897932384626433832795)
    3

    You can see all static methods of System.Math using Get-Member:
    --
    greetings
    dreeschkind

     
    dreeschkind, Jul 27, 2006
    #5
  6. Rob Pettrey

    Babu VT Guest

    Hi Rob,

    May be this is useful...
    http://msdn2.microsoft.com/en-us/library/system.decimal.toint32.aspx

    rgds
    Babu

     
    Babu VT, Jul 27, 2006
    #6
  7. Rob Pettrey

    Rob Pettrey Guest

    Thank you. I did actually find the same truncate command this morning, and
    tried it, and it works! Yea!

    $wom=[math]::truncate($dom/7)

    Does it matter if the command is on the left or the right? Is there a rule,
    or principle?

    Questions:

    How does one find these things? I found it only by googling "powershell
    truncate" on a whim. Is there somewhere a repository, with examples? I have
    tried the internal help, but it seems that you need to know what you are
    looking for before you can look for it.

    It also seems that you need to be an object-oriented programmer to
    understand all this. (About the only things I have written were eons ago, in
    basic - or cobol - or gasp! DCL!) Where can a network guy go to find and
    understand these things, who has never written a stitch of code in visual
    basic, let alone C/C++/C#/C flat? ;>)


     
    Rob Pettrey, Jul 27, 2006
    #7
  8. Rob Pettrey

    dreeschkind Guest

    Well, since PowerShell is based on .NET you can use all Types/Objects and
    Methods (think: object orientation) from the .NET framework, so any good .NET
    book/tutorial might help.

    Also have a look at some PowerShell blogs:
    http://blogs.msdn.com/powershell/ (official blog)
    http://mow001.blogspot.com/
    http://monadblog.blogspot.com/
    (google: MSH, Monad, PowerShell)

    There are also some good links in Wikipedia to get you started:
    http://en.wikipedia.org/wiki/Powershell

    And there is the PowerShell script center:
    http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

    You might also have a look at some books that come out this year, but don't
    buy the Monad book by Andy Oakley (ISBN 0-596-10009-4) it is based on an
    older release and a little outdated.
    This one seems to be more interesting, it's not out yet, but you can
    download the first chapter for free: http://www.sapienpress.com/powershell.asp

    Also note that most PowerShellers are now in the new newsgroup:
    microsoft.public.windows.powershell

    --
    greetings
    dreeschkind

     
    dreeschkind, Jul 27, 2006
    #8
  9. Rob Pettrey

    dreeschkind Guest

    Sorry, I don't understand your question.
    What do you mean by command on the left or the right?

    [math] is a .NET type (actually it is short for System.Math)
    The two :: mean that you are calling it's static method called Truncate().
    There are also monstatic methods, you use a single . to call them.
    $wom= simply means that the result of the truncate method is stored in
    the variable $wom.

    Regarding the PowerShell online help, these are just XML files in the
    PowerShell directory, so you could basically search them with any tool you
    like. There are some PowerShell scripts that can do that very nicely, have a
    look at the PowerShell blogs or the PowerShell newsgroup for that. Someone
    has also written a helpfile browser, but I hadn't have a chance to test it
    yet: http://secretgeek.net/shinypower.asp
     
    dreeschkind, Jul 27, 2006
    #9
  10. Rob Pettrey

    Rob Pettrey Guest

    For instance:

    [int]$wom=$dom/7
    or
    $wom=[int]$dom/7

    the first example has [int] on the left side of the eqation, second is on
    the right side.

    Thank you for your overview-type explanations. I will try to read up on
    these things.

     
    Rob Pettrey, Jul 27, 2006
    #10
  11. Rob Pettrey

    dreeschkind Guest


    In the first example you first explicitly define the type of the variable
    $wom as [int].
    When the calculated value of right side of the equation (double) is assigned
    to that integer variable, it will be automatically casted to the type of the
    variable $wom (integer).

    In your second example you don't define the type of $wom at all.
    This is ok, because PowerShell can dynamically recognize the type of the
    variable based on what you store in it. This mechanism is called "duck
    typing" (http://en.wikipedia.org/wiki/Duck_typing).
    When the right side of the equation is evaluated, the type of the result of
    the division is of type double. This double value is then explicitly casted
    to [int]. Then this integer value is assigned to your variable $wom, which
    will then be of type integer because of the "duck typing" mentioned above. If
    you would leave out this explicit cast to [int], your variable $wom would be
    of type [double].
    Note that this "duck typing" is only works when you haven't explicitly
    defined the type of of the variable like in your example. Once you define the
    type explicitly, the variable will keep that type.

    Hope that helps.

    PS: I need to correct my self, the help files in the PowerShell directory
    are actually plain txt files not XML files as erroneously mentioned in my
    other posting.
     
    dreeschkind, Jul 27, 2006
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.