Powershell is there an easy way to hide int 5 to line five or 68 to sixty-eight?

I am trying to figure out if there is an easy way to convert numbers to words, take 9 and convert them to nine.

+3


source to share


2 answers


There is a great .NET library called Humanizer that can do just that. I haven't tried this yet, but it looks like there is a PowerShell for it . I suspect this will do exactly what you need.



+4


source


This was asked about .NET / C #; you can put this in a class and use it Add-Type

in powershell to make this work.

.NET convert number to string representation (1 to one, two to two, etc.)

Maybe something like this (untested):



$class = @"
    public class Num2Word
    {
        public static string NumberToText( int n)
          {
           if ( n < 0 )
              return "Minus " + NumberToText(-n);
           else if ( n == 0 )
              return "";
           else if ( n <= 19 )
              return new string[] {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", 
                 "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", 
                 "Seventeen", "Eighteen", "Nineteen"}[n-1] + " ";
           else if ( n <= 99 )
              return new string[] {"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", 
                 "Eighty", "Ninety"}[n / 10 - 2] + " " + NumberToText(n % 10);
           else if ( n <= 199 )
              return "One Hundred " + NumberToText(n % 100);
           else if ( n <= 999 )
              return NumberToText(n / 100) + "Hundreds " + NumberToText(n % 100);
           else if ( n <= 1999 )
              return "One Thousand " + NumberToText(n % 1000);
           else if ( n <= 999999 )
              return NumberToText(n / 1000) + "Thousands " + NumberToText(n % 1000);
           else if ( n <= 1999999 )
              return "One Million " + NumberToText(n % 1000000);
           else if ( n <= 999999999)
              return NumberToText(n / 1000000) + "Millions " + NumberToText(n % 1000000);
           else if ( n <= 1999999999 )
              return "One Billion " + NumberToText(n % 1000000000);
           else 
              return NumberToText(n / 1000000000) + "Billions " + NumberToText(n % 1000000000);
        }
    }
@"

Add-Type -TypeDefinition $class

[Num2Word]::NumberToText(555)

      

No reason you couldn't write this as pure powershell, but it was already written!

0


source







All Articles