PowerTip: Use PowerShell to Get Length of a Number

PowerTip: Use PowerShell to Get Length of a Number

  • Comments 6
  • Likes

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:


Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • ???? ----> (∞).ToString().Length

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

  • hi, another solution: (PS2) > '12345'.length

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

  • 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