Formatting Large Numbers with .NET

You can use Log10 to determine the correct break. Something like this could work: double number = 4316000; int mag = (int)(Math.Floor(Math.Log10(number))/3); // Truncates to 6, divides to 2 double divisor = Math.Pow(10, mag*3); double shortNumber = number / divisor; string suffix; switch(mag) { case 0: suffix = string.Empty; break; case 1: suffix = “k”; … Read more

How to capitalize the first character of each word, or the first character of a whole string, with C#?

As discussed in the comments of @miguel’s answer, you can use TextInfo.ToTitleCase which has been available since .NET 1.1. Here is some code corresponding to your example: string lipsum1 = “Lorem lipsum et”; // Creates a TextInfo based on the “en-US” culture. TextInfo textInfo = new CultureInfo(“en-US”,false).TextInfo; // Changes a string to titlecase. Console.WriteLine(“\”{0}\” to … Read more

How do I convert CamelCase into human-readable names in Java?

This works with your testcases: static String splitCamelCase(String s) { return s.replaceAll( String.format(“%s|%s|%s”, “(?<=[A-Z])(?=[A-Z][a-z])”, “(?<=[^A-Z])(?=[A-Z])”, “(?<=[A-Za-z])(?=[^A-Za-z])” ), ” ” ); } Here’s a test harness: String[] tests = { “lowercase”, // [lowercase] “Class”, // [Class] “MyClass”, // [My Class] “HTML”, // [HTML] “PDFLoader”, // [PDF Loader] “AString”, // [A String] “SimpleXMLParser”, // [Simple XML Parser] … Read more