Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class VAInline
- {
- public static string Humanize(decimal? value)
- {
- if (value == null)
- {
- return null;
- }
- if (value < 0)
- {
- // We don't handle negatives at the moment
- return "" + value;
- }
- if (value == 0)
- {
- return "zero";
- }
- if (value < 10)
- {
- // Work out how many 0s to begin with
- int numzeros = -1;
- decimal testval = (decimal)value;
- while (value < 1)
- {
- value *= 10;
- numzeros++;
- }
- // Now round it to 2sf
- return (Math.Round((double)value * 10) / (Math.Pow(10, numzeros + 2))).ToString();
- }
- int number;
- int nextDigit;
- string order;
- int digits = (int)Math.Log10((double)value);
- if (digits < 3)
- {
- // Units
- number = (int)value;
- order = "";
- nextDigit = 0;
- }
- else if (digits < 6)
- {
- // Thousands
- number = (int)(value / 1000);
- order = "thousand";
- nextDigit = (((int)value) - (number * 1000)) / 100;
- }
- else if (digits < 9)
- {
- // Millions
- number = (int)(value / 1000000);
- order = "million";
- nextDigit = (((int)value) - (number * 1000000)) / 100000;
- }
- else if (digits < 12)
- {
- // Billions
- number = (int)(value / 1000000000);
- order = "billion";
- nextDigit = (int)(((long)value) - ((long)number * 1000000000)) / 100000000;
- }
- else if (digits < 15)
- {
- // Trillions
- number = (int)(value / 1000000000000);
- order = "trillion";
- nextDigit = (int)(((long)value) - (int)((number * 1000000000000)) / 100000000000);
- }
- else
- {
- // Quadrillions
- number = (int)(value / 1000000000000000);
- order = "quadrillion";
- nextDigit = (int)(((long)value) - (int)((number * 1000000000000000)) / 100000000000000);
- }
- if (order == "")
- {
- return "" + number;
- }
- else
- {
- // See if we have an exact match
- if (((long)(((decimal)value) / (decimal)Math.Pow(10, digits - 1))) * (decimal)(Math.Pow(10, digits - 1)) == value)
- {
- return "" + number + " " + order;
- }
- if (number > 60)
- {
- if (nextDigit < 6)
- {
- return "Over " + number + " " + order;
- }
- else
- {
- return "Nearly " + (number + 1) + " " + order;
- }
- }
- }
- switch (nextDigit)
- {
- case 0:
- return "just over " + number + " " + order;
- case 1:
- return "over " + number + " " + order;
- case 2:
- return "well over " + number + " " + order;
- case 3:
- return "on the way to " + number + " and a half " + order;
- case 4:
- return "nearly " + number + " and a half " + order;
- case 5:
- return "around " + number + " and a half " + order;
- case 6:
- return "just over " + number + " and a half " + order;
- case 7:
- return "well over " + number + " and a half " + order;
- case 8:
- return "on the way to " + (number + 1) + " " + order;
- case 9:
- return "nearly " + (number + 1) + " " + order;
- default:
- return "around " + number + " " + order;
- }
- }
- public void main()
- {
- String[] inputVars = VA.GetText("~HumanizeNumber").Split(',');
- foreach (string i in inputVars)
- {
- String[] input = i.Split(':');
- decimal? number = 0;
- switch(input[0])
- {
- case "INT":
- number = Convert.ToDecimal(VA.GetInt(input[1]));
- break;
- case "SMALL":
- number = Convert.ToDecimal(VA.GetSmallInt(input[1]));
- break;
- case "DEC":
- number = VA.GetDecimal(input[1]).Value;
- break;
- }
- VA.SetText(input[1] + input[0] + "Humanized", Humanize(number));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement