Changeset 4068 for trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/ToolBox.Formatting
- Timestamp:
- 07/22/10 00:44:01 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/ToolBox.Formatting
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/ToolBox.Formatting/TextFormat.cs
r2768 r4068 1 1 using System; 2 using System.Collections.Generic;3 using System.Text;4 2 5 namespace ToolBox.Formatting 6 { 7 [Serializable()] 8 public enum TextFormat 9 { 10 // ------------------------------------------------------------------ 11 /// <summary> 12 /// No formatting is applied. 13 /// </summary> 14 // ------------------------------------------------------------------ 15 String, 3 namespace ToolBox.Formatting { 4 [Serializable()] 5 public enum TextFormat { 6 // ------------------------------------------------------------------ 7 /// <summary> 8 /// No formatting is applied. 9 /// </summary> 10 // ------------------------------------------------------------------ 11 String, 16 12 17 18 19 20 21 22 13 // ------------------------------------------------------------------ 14 /// <summary> 15 /// Culturally aware currency format. 16 /// </summary> 17 // ------------------------------------------------------------------ 18 Currency, 23 19 24 25 26 27 28 29 30 20 // ------------------------------------------------------------------ 21 /// <summary> 22 /// Only supports integral numbers. Displays a string using decimal 23 /// digits preceded by a minus sign if negative. 24 /// </summary> 25 // ------------------------------------------------------------------ 26 Decimal, 31 27 32 33 34 35 36 37 38 28 // ------------------------------------------------------------------ 29 /// <summary> 30 /// Displays numbers in the form ±d.ddddddE±dd where d is a decimal 31 /// digit. 32 /// </summary> 33 // ------------------------------------------------------------------ 34 ScientificNotation, 39 35 40 41 42 43 44 45 46 36 // ------------------------------------------------------------------ 37 /// <summary> 38 /// Displays a series of decimal digits with a decimal point and 39 /// additional digits. 40 /// </summary> 41 // ------------------------------------------------------------------ 42 FixedPoint, 47 43 48 49 50 51 52 53 54 44 // ------------------------------------------------------------------ 45 /// <summary> 46 /// Displays either as a fixed-point or scientific notation based 47 /// on the size of the number. 48 /// </summary> 49 // ------------------------------------------------------------------ 50 General, 55 51 56 57 58 59 60 61 62 52 // ------------------------------------------------------------------ 53 /// <summary> 54 /// Similar to fixed point but uses a separator character (such as ,) 55 /// for groups of digits. 56 /// </summary> 57 // ------------------------------------------------------------------ 58 Number, 63 59 64 65 66 67 68 69 60 // ------------------------------------------------------------------ 61 /// <summary> 62 /// Multiplies the number by 100 and displays with a percent symbol. 63 /// </summary> 64 // ------------------------------------------------------------------ 65 Percentage, 70 66 71 72 73 74 75 76 77 67 // ------------------------------------------------------------------ 68 /// <summary> 69 /// Formats a floating-point number so that it can be successfully 70 /// converted back to its original value. 71 /// </summary> 72 // ------------------------------------------------------------------ 73 RoundTrip, 78 74 79 80 81 82 83 84 85 75 // ------------------------------------------------------------------ 76 /// <summary> 77 /// Displays an integral number using the base-16 number system. 78 /// </summary> 79 // ------------------------------------------------------------------ 80 Hexadecimal 81 } 86 82 } -
trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/ToolBox.Formatting/TextFormatter.cs
r2768 r4068 1 1 using System; 2 using System.Collections.Generic;3 using System.Text;4 2 5 namespace ToolBox.Formatting 6 { 7 public class TextFormatter 8 { 9 // ------------------------------------------------------------------ 10 /// <summary> 11 /// Returns a string that's formatted using the specified format for 12 /// the number specified. 13 /// </summary> 14 /// <param name="format">TextFormat</param> 15 /// <param name="number">double</param> 16 /// <param name="significantDigits">int</param> 17 /// <returns>string</returns> 18 // ------------------------------------------------------------------ 19 public static string Format( 20 TextFormat format, 21 double number, 22 int significantDigits) 23 { 24 return Format(format, number.ToString(), significantDigits); 3 namespace ToolBox.Formatting { 4 public class TextFormatter { 5 // ------------------------------------------------------------------ 6 /// <summary> 7 /// Returns a string that's formatted using the specified format for 8 /// the number specified. 9 /// </summary> 10 /// <param name="format">TextFormat</param> 11 /// <param name="number">double</param> 12 /// <param name="significantDigits">int</param> 13 /// <returns>string</returns> 14 // ------------------------------------------------------------------ 15 public static string Format( 16 TextFormat format, 17 double number, 18 int significantDigits) { 19 return Format(format, number.ToString(), significantDigits); 20 } 21 22 // ------------------------------------------------------------------ 23 /// <summary> 24 /// Returns a string that's formatted using the specified format for 25 /// the text specified. If a format other than 'String' is specified, 26 /// the text is first converted to a double, then the resultant string 27 /// that represents the number in the specified format is returned. If 28 /// the conversion from the text to a double fails, no exception is 29 /// thrown. The supplied text is simply returned. 30 /// </summary> 31 /// <param name="format">TextFormat</param> 32 /// <param name="text">string</param> 33 /// <param name="significantDigits">int</param> 34 /// <returns>string</returns> 35 // ------------------------------------------------------------------ 36 public static string Format( 37 TextFormat format, 38 string text, 39 int significantDigits) { 40 double myNumber = double.MaxValue; 41 string result = ""; 42 43 // Only if the format specified isn't a string, 44 // then try to convert the text to a number. If the 45 // conversion fails, just return the text. 46 if (format != TextFormat.String) { 47 try { 48 myNumber = double.Parse(text); 25 49 } 50 catch { 51 return text; 52 } 53 } 26 54 27 // ------------------------------------------------------------------ 28 /// <summary> 29 /// Returns a string that's formatted using the specified format for 30 /// the text specified. If a format other than 'String' is specified, 31 /// the text is first converted to a double, then the resultant string 32 /// that represents the number in the specified format is returned. If 33 /// the conversion from the text to a double fails, no exception is 34 /// thrown. The supplied text is simply returned. 35 /// </summary> 36 /// <param name="format">TextFormat</param> 37 /// <param name="text">string</param> 38 /// <param name="significantDigits">int</param> 39 /// <returns>string</returns> 40 // ------------------------------------------------------------------ 41 public static string Format( 42 TextFormat format, 43 string text, 44 int significantDigits) 45 { 46 double myNumber = double.MaxValue; 47 string result = ""; 55 // Used for the number of sig digs. For one sig dig, this 56 // will be #.#. For two sig digs, it'll be #.##. 57 string sigDigIdentifier = "#"; 48 58 49 // Only if the format specified isn't a string, 50 // then try to convert the text to a number. If the 51 // conversion fails, just return the text. 52 if (format != TextFormat.String) 53 { 54 try 55 { 56 myNumber = double.Parse(text); 57 } 58 catch 59 { 60 return text; 61 } 62 } 59 if (significantDigits > 0) { 60 sigDigIdentifier = "#."; 61 for (int i = 0; i < significantDigits; i++) { 62 sigDigIdentifier += "#"; 63 } 64 } 65 switch (format) { 66 case TextFormat.String: 67 result = text; 68 break; 63 69 64 // Used for the number of sig digs. For one sig dig, this65 // will be #.#. For two sig digs, it'll be #.##.66 string sigDigIdentifier = "#";70 case TextFormat.Currency: 71 result = String.Format("{0:c}", myNumber); 72 break; 67 73 68 if (significantDigits > 0) 69 { 70 sigDigIdentifier = "#."; 71 for (int i = 0; i < significantDigits; i++) 72 { 73 sigDigIdentifier += "#"; 74 } 75 } 76 switch (format) 77 { 78 case TextFormat.String: 79 result = text; 80 break; 74 case TextFormat.Decimal: 75 result = String.Format("{0:d}", (int)myNumber); 76 break; 81 77 82 case TextFormat.Currency:83 result = String.Format("{0:c}", myNumber);84 78 case TextFormat.FixedPoint: 79 result = String.Format("{0:f}", myNumber); 80 break; 85 81 86 case TextFormat.Decimal:87 result = String.Format("{0:d}", (int)myNumber);88 82 case TextFormat.General: 83 result = String.Format("{0:g}", myNumber); 84 break; 89 85 90 case TextFormat.FixedPoint:91 result = String.Format("{0:f}",myNumber);92 86 case TextFormat.Hexadecimal: 87 result = String.Format("{0:x}", (int)myNumber); 88 break; 93 89 94 case TextFormat.General: 95 result = String.Format("{0:g}", myNumber); 96 break; 90 case TextFormat.Number: 91 //result = String.Format("{0:n}", myNumber); 92 result = myNumber.ToString(sigDigIdentifier); 93 break; 97 94 98 case TextFormat.Hexadecimal:99 result = String.Format("{0:x}", (int)myNumber);100 95 case TextFormat.Percentage: 96 result = String.Format("{0:p}", myNumber); 97 break; 101 98 102 case TextFormat.Number: 103 //result = String.Format("{0:n}", myNumber); 104 result = myNumber.ToString(sigDigIdentifier); 105 break; 99 case TextFormat.RoundTrip: 100 result = String.Format("{0:r}", myNumber); 101 break; 106 102 107 case TextFormat.Percentage: 108 result = String.Format("{0:p}", myNumber); 109 break; 103 case TextFormat.ScientificNotation: 104 //result = String.Format("{0:e}", myNumber); 105 result = myNumber.ToString(sigDigIdentifier + "E+0"); 106 break; 110 107 111 case TextFormat.RoundTrip: 112 result = String.Format("{0:r}", myNumber); 113 break; 114 115 case TextFormat.ScientificNotation: 116 //result = String.Format("{0:e}", myNumber); 117 result = myNumber.ToString(sigDigIdentifier + "E+0"); 118 break; 119 120 default: result = text; 121 break; 122 } 123 return result; 124 } 108 default: result = text; 109 break; 110 } 111 return result; 125 112 } 113 } 126 114 }
Note: See TracChangeset
for help on using the changeset viewer.