Padding numerical values in strings
// add the desired number of "0"s for padding in front of the "."
// add the desired number of "0"s for precision after the "." (including rounding up/down)
string text = String.Format("Heading: {0:000.000}", heading);
// 12.3456789 = Heading: 012.346
// -1.2345678 = Heading: -001.234
// extend formatting to include cases for positive, negative and zero values.
string text = String.Format("Heading: {0:+00.0;-00.0;±00.0}", heading)
// 12.3456789 = Heading: +12.3
// 0.0000000 = Heading: ±00.0
// -1.2345678 = Heading: -01.2
Format date and time nicely
// This formats the current date/time, but you can pass a custom DateTime object, if you like.
public static string DateTimeFormat()
{
DateTime now = DateTime.Now;
return now.ToString("yyyy-MM-dd HH:mm:ss.fff");
}
// 2024-09-30 22:58:04.338
Add some formatting to your console output
// See at which time a method was called, if you reference the previous DateTimeFormat() method.
// Quickly filter your console output by method name.
// See output from different components by colors.
public class Format {
public static string Debug(string methodName, string logContent, Color classColor)
{
string hexColor = ColorUtility.ToHtmlStringRGB(classColor);
return $"[{DateTimeFormat()}:<color=#{hexColor}>{methodName}]</color> {logContent}";
}
}
// Call in your Debug.Log(Format.Debug("", "", Color.cyan));
// [2024-09-30 22:58:04.338:TestFunction] Test successful