Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.3/HeuristicLab.ExtLibs/HeuristicLab.Netron/3.0.2672.12446/Netron.Diagramming.Core-3.0.2672.12446/ToolBox.Formatting/TextFormatter.cs @ 5447

Last change on this file since 5447 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

File size: 3.7 KB
Line 
1using System;
2
3namespace 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);
49        }
50        catch {
51          return text;
52        }
53      }
54
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 = "#";
58
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;
69
70        case TextFormat.Currency:
71          result = String.Format("{0:c}", myNumber);
72          break;
73
74        case TextFormat.Decimal:
75          result = String.Format("{0:d}", (int)myNumber);
76          break;
77
78        case TextFormat.FixedPoint:
79          result = String.Format("{0:f}", myNumber);
80          break;
81
82        case TextFormat.General:
83          result = String.Format("{0:g}", myNumber);
84          break;
85
86        case TextFormat.Hexadecimal:
87          result = String.Format("{0:x}", (int)myNumber);
88          break;
89
90        case TextFormat.Number:
91          //result = String.Format("{0:n}", myNumber);
92          result = myNumber.ToString(sigDigIdentifier);
93          break;
94
95        case TextFormat.Percentage:
96          result = String.Format("{0:p}", myNumber);
97          break;
98
99        case TextFormat.RoundTrip:
100          result = String.Format("{0:r}", myNumber);
101          break;
102
103        case TextFormat.ScientificNotation:
104          //result = String.Format("{0:e}", myNumber);
105          result = myNumber.ToString(sigDigIdentifier + "E+0");
106          break;
107
108        default: result = text;
109          break;
110      }
111      return result;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.