Advertisement
SemlerPDX

EDDP Humanize Numbers as VoiceAttack Inline Function in C# (example)

Jan 1st, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.68 KB | None | 0 0
  1. using System;
  2.  
  3. public class VAInline
  4. {
  5.     public static string Humanize(decimal? value)
  6.     {
  7.         if (value == null)
  8.         {
  9.             return null;
  10.         }
  11.  
  12.         if (value < 0)
  13.         {
  14.             // We don't handle negatives at the moment
  15.             return "" + value;
  16.         }
  17.  
  18.         if (value == 0)
  19.         {
  20.             return "zero";
  21.         }
  22.  
  23.         if (value < 10)
  24.         {
  25.             // Work out how many 0s to begin with
  26.             int numzeros = -1;
  27.             decimal testval = (decimal)value;
  28.             while (value < 1)
  29.             {
  30.                 value *= 10;
  31.                 numzeros++;
  32.             }
  33.             // Now round it to 2sf
  34.             return (Math.Round((double)value * 10) / (Math.Pow(10, numzeros + 2))).ToString();
  35.         }
  36.  
  37.         int number;
  38.         int nextDigit;
  39.         string order;
  40.         int digits = (int)Math.Log10((double)value);
  41.         if (digits < 3)
  42.         {
  43.             // Units
  44.             number = (int)value;
  45.             order = "";
  46.             nextDigit = 0;
  47.         }
  48.         else if (digits < 6)
  49.         {
  50.             // Thousands
  51.             number = (int)(value / 1000);
  52.             order = "thousand";
  53.             nextDigit = (((int)value) - (number * 1000)) / 100;
  54.         }
  55.         else if (digits < 9)
  56.         {
  57.             // Millions
  58.             number = (int)(value / 1000000);
  59.             order = "million";
  60.             nextDigit = (((int)value) - (number * 1000000)) / 100000;
  61.         }
  62.         else if (digits < 12)
  63.         {
  64.             // Billions
  65.             number = (int)(value / 1000000000);
  66.             order = "billion";
  67.             nextDigit = (int)(((long)value) - ((long)number * 1000000000)) / 100000000;
  68.         }
  69.         else if (digits < 15)
  70.         {
  71.             // Trillions
  72.             number = (int)(value / 1000000000000);
  73.             order = "trillion";
  74.             nextDigit = (int)(((long)value) - (int)((number * 1000000000000)) / 100000000000);
  75.         }
  76.         else
  77.         {
  78.             // Quadrillions
  79.             number = (int)(value / 1000000000000000);
  80.             order = "quadrillion";
  81.             nextDigit = (int)(((long)value) - (int)((number * 1000000000000000)) / 100000000000000);
  82.         }
  83.  
  84.         if (order == "")
  85.         {
  86.             return "" + number;
  87.         }
  88.         else
  89.         {
  90.             // See if we have an exact match
  91.             if (((long)(((decimal)value) / (decimal)Math.Pow(10, digits - 1))) * (decimal)(Math.Pow(10, digits - 1)) == value)
  92.             {
  93.                 return "" + number + " " + order;
  94.             }
  95.             if (number > 60)
  96.             {
  97.                 if (nextDigit < 6)
  98.                 {
  99.                     return "Over " + number + " " + order;
  100.                 }
  101.                 else
  102.                 {
  103.                     return "Nearly " + (number + 1) + " " + order;
  104.                 }
  105.             }
  106.         }
  107.         switch (nextDigit)
  108.         {
  109.             case 0:
  110.                 return "just over " + number + " " + order;
  111.             case 1:
  112.                 return "over " + number + " " + order;
  113.             case 2:
  114.                 return "well over " + number + " " + order;
  115.             case 3:
  116.                 return "on the way to " + number + " and a half " + order;
  117.             case 4:
  118.                 return "nearly " + number + " and a half " + order;
  119.             case 5:
  120.                 return "around " + number + " and a half " + order;
  121.             case 6:
  122.                 return "just over " + number + " and a half " + order;
  123.             case 7:
  124.                 return "well over " + number + " and a half " + order;
  125.             case 8:
  126.                 return "on the way to " + (number + 1) + " " + order;
  127.             case 9:
  128.                 return "nearly " + (number + 1) + " " + order;
  129.             default:
  130.                 return "around " + number + " " + order;
  131.         }
  132.     }
  133.     public void main()
  134.     {
  135.         String[] inputVars = VA.GetText("~HumanizeNumber").Split(',');
  136.         foreach (string i in inputVars)
  137.         {
  138.             String[] input = i.Split(':');
  139.             decimal? number = 0;
  140.             switch(input[0])
  141.             {
  142.                 case "INT":
  143.                     number = Convert.ToDecimal(VA.GetInt(input[1]));
  144.                     break;
  145.                 case "SMALL":
  146.                     number = Convert.ToDecimal(VA.GetSmallInt(input[1]));
  147.                     break;
  148.                 case "DEC":
  149.                     number = VA.GetDecimal(input[1]).Value;
  150.                     break;
  151.             }
  152.             VA.SetText(input[1] + input[0] + "Humanized", Humanize(number));
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement