Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Encrypting Editor Notepad Replacement

, 28 Jul 2014
A C# .NET 3.5 (Win7) Windows Forms Application with source code
CryptPad-SourceOnly.zip
CryptPad
CryptPad (read-only).lnk
CryptPad.suo
CryptPad
CryptPad.csproj.user
CryptPad.ico
CryptPad.png
Properties
Settings.settings
hack.safe
Script.CryptPad
SendTo.lnk
Settings.lnk
CryptPad.zip
CryptPad (read-only).lnk
CryptPad.suo
CryptPad.csproj.user
CryptPad.ico
CryptPad.png
Settings.settings
hack.safe
Script.CryptPad
SendTo.lnk
Settings.lnk
using System;

namespace AbbreviateApproximate
{
	/// <summary> Abbreviate positive numbers to 4 character string
	/// and Approximate positive numbers from string using powers of 1000 not 2^10
	/// </summary>
	class Abbr
	{
		/// <summary> Return the abbreviated (4 characters or less) approximate value as a string
		/// Automatic override type cast
		/// </summary>
		/// <param name="number">The numeric value to convert to string</param> 
		public static String Appr (Int64 number)
		{
			if (0 > number)
				return "";
			return Appr ((ulong) number);
		}

		/// <summary>
		/// Return the abbreviated (4 characters or less) approximate rounded value as a string
		/// </summary>
		/// <param name="number">The numeric value to convert to string</param> 
		public static String Appr (UInt64 number)
		{
			if (1000 > number)
				return number.ToString ();
			// don't overflow very large number
			ulong r = number % 1000;
			number = number / 1000;
			if (r > 500)
				++number;
			if (1000 > number)
				return number.ToString () + "K";
			number = (number + 500) / 1000;
			if (1000 > number)
				return number.ToString () + "M";
			number = (number + 500) / 1000;
			if (1000 > number)
				return number.ToString () + "G";
			number = (number + 500) / 1000;
			if (1000 > number)
				return number.ToString () + "T";
			number = (number + 500) / 1000;
			if (1000 > number)
				return number.ToString () + "P";
			number = (number + 500) / 1000;
			if (19 == number)
				--number;
			return number.ToString () + "E";
		}

		/// <summary>
		/// Return the approximate value from an abbreviated string
		/// </summary>
		/// <param name="str">The string value to convert to numeric</param> 
		public static Int32 ApprI32 (String str)
		{
			UInt64 t;
			t = ApprU64 (str);
			if (Int32.MaxValue < t)
				return Int32.MaxValue;
			return (Int32) t;
		}

		/// <summary>
		/// Return the approximate value from an abbreviated string
		/// </summary>
		/// <param name="str">The string value to convert to numeric</param> 
		public static UInt32 ApprU32 (String str)
		{
			UInt64 t;
			t = ApprU64 (str);
			if (UInt32.MaxValue < t)
				return UInt32.MaxValue;
			return (UInt32) t;
		}

		/// <summary>
		/// Return the approximate value from an abbreviated string
		/// </summary>
		/// <param name="str">The string value to convert to numeric</param> 
		public static Int64 ApprI64 (String str)
		{
			UInt64 t;
			t = ApprU64 (str);
			if (Int64.MaxValue < t)
				return Int64.MaxValue;
			return (Int64) t;
		}

		/// <summary>
		/// Return the approximate value from an abbreviated string
		/// </summary>
		/// <param name="str">The string value to convert to numeric</param> 
		public static UInt64 ApprU64 (String str)
		{
			UInt64 t = 0;
			if (null == str)
				return 0;
			int i = str.Length - 1;
			if (i < 0)
				return 0;
			char suffix = str[i];
			if (char.IsDigit (suffix))
			{
				UInt64.TryParse (str, out t);
				return t;
			}
			if (!UInt64.TryParse (str.Substring (0, i), out t))
				return 0;
			t *= 1000;
			if ('K' == suffix)
				return t;
			t *= 1000;
			if ('M' == suffix)
				return t;
			t *= 1000;
			if ('G' == suffix)
				return t;
			t *= 1000;
			if ('T' == suffix)
				return t;
			t *= 1000;
			if ('P' == suffix)
				return t;
			t *= 1000;
			if ('E' == suffix)
				return t;
			return 0;
		}

		/// <summary>
		/// Demonstrate and validate a range of numbers
		/// </summary>
		public static void Validate ()
		{
			for (UInt64 t = 0; ; )
			{
				Validate (t);
				if (999 > t)
					t = t * 10 + 9;
				else if (t < Int64.MaxValue)
					t *= 1000;
				else
					break;
			}
			Console.WriteLine ("\tInt64.MaxValue");
			Validate (Int64.MaxValue);
			Console.WriteLine ("\tUInt64.MaxValue");
			Validate (UInt64.MaxValue);
		}

		/// <summary>
		/// Demonstrate and validate a numeric value
		/// </summary>
		/// <param name="str">The string value to convert to numeric</param> 
		public static void Validate (UInt64 t)
		{
			string str = Appr (t);
			Console.WriteLine (t.ToString ().PadLeft (20) + " = " + str.PadLeft(4) + " = " + ApprU64 (str).ToString().PadLeft(20));
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Frank T. Clark
Systems Engineer Three Angels Broadcasting Network
United States United States
I am a Software Systems Design Engineer experienced with IEEE standards and the entire IEEE software development life cycle. Concept Exploration, Requirements, Design, Implementation, Test, Installation and Checkout, Operation and Maintenance. I enjoy working with people and solving customer problems.
 
I am currently employed as Information Technology Staff in the religious satellite broadcasting industry.

| Advertise | Privacy | Mobile
Web01 | 2.8.140902.1 | Last Updated 28 Jul 2014
Article Copyright 2013 by Frank T. Clark
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid