Skip to content

Instantly share code, notes, and snippets.

@lazywinadmin
Created November 18, 2015 15:46
Show Gist options
  • Save lazywinadmin/b4e77c305d47878381d7 to your computer and use it in GitHub Desktop.
Save lazywinadmin/b4e77c305d47878381d7 to your computer and use it in GitHub Desktop.
Add a String method
$a = 1257657656
$a = $a | Add-Member -MemberType ScriptMethod -Name tostring -Force -Value { param($Unit = 1MB, $Digits =1, $Suffix=' MB') "{0:n$Digits}$Suffix" -f ($this/($Unit)) } -PassThru
<#
PS> $a
1.199,4 MB
PS> $a.ToString(1GB, 0, ' GB')
1 GB
PS> $a.ToString(1KB, 2, ' KB')
1.228.181,30 KB
PS> $a -eq 1257657656
True
PS> $a -eq 1257657657
False
PS> $a.GetType().Name
Int32
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment