PowerTip: Use PowerShell to Get Length of a Number

Summary:  Use Windows PowerShell to easily get the length of a number.

Hey, Scripting Guy! Question I want to know the length of a number, but when I use the Length property, it comes back as 1.
          How can I get the actual length?

Hey, Scripting Guy! Answer First convert the number to a string, and then get the length of the string:


Comments (5)

  1. WhyNot says:

    ???? —-> (∞).ToString().Length

  2. Ed Wilson says:

    @whyNot that is exactly what my code up there does. Convert to string, then gets the length.

  3. Walid Toumi says:

    hi, another solution: (PS2) > ‘12345’.length

  4. Sean Kearney says:

    @Ed Looks like @whynot is trying to overflow the system by getting the length of Infinity 🙂 Sean

  5. Cordell Lawrence says:

    if you’re doing a lot of these, the string creation may be a bit taxing on memory – i.e. a string will be created for each number simply to get the length and then thrown away – My suggestion is that if you’re doing lots of these you can use: [Math]::Log10(1234)
    + 1

Skip to main content