Archive

Posts Tagged ‘Number’

DateTime Formatter and Numeric Formatter

February 6th, 2009 Elze 1 comment

Number Juggle

The .NET Micro Framework doesn’t support the formatting of it’s bigger .NET brothers. I hope/think it will be supported on the next .NET Micro Framework version but for the mean time I made two formatting classes: One class that formats DateTime objects the second formats numbers.

 

DateFormatter Class:

The DateFormatter Class has one function, the Format function that accepts a Format string and a DateTime object.

The function follows the rules of the strftime function found in PHP. More information about this format can be found on this page: http://nl.php.net/strftime

Example code:

DateTime t = new DateTime(2009, 2, 1, 14, 10, 00);
Debug.Print(ElzeKool.Utilities.DateFormatter.Format("%d/%m/%Y  %C", t));

The DateFormatter Class can be downloaded here: DateFormatter.cs



NumericFormatter Class:

The NumericFormatter Class has one function, the Format function that accepts a Format string and a number.
The function follows the C# Custom Formatting, the specification can be found on MSDN:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

The Per mille symbol isn’t supported as this is not included in the UTF8 character set.
Besides the custom formatting strings the function also supports the X,x,D,d numeric formatting specifiers

Example code:

// Outputs 8.6E004
Debug.Print(ElzeKool.Utilities.NumberFormatter.Format("0.###E-000", 86000));

// Outputs 86000
Debug.Print(ElzeKool.Utilities.NumberFormatter.Format("##;(##)", 86000));

// Outputs (86000)
Debug.Print(ElzeKool.Utilities.NumberFormatter.Format("##;(##)", -86000));

// Outputs 00ABCDEF
Debug.Print(ElzeKool.Utilities.NumberFormatter.Format("H8", 0xabcdef));

The NumericFormatter Class can be downloaded here: NumericFormatter.cs