File size: 1,895 Bytes
b1b3bae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/// ------------------------------------------------------
/// SwarmOps - Numeric and heuristic optimization for C#
/// Copyright (C) 2003-2011 Magnus Erik Hvass Pedersen.
/// Please see the file license.txt for license details.
/// SwarmOps on the internet: http://www.Hvass-Labs.org/
/// ------------------------------------------------------

using System;
using System.Globalization;

namespace SwarmOps
{
    public static partial class Tools
    {
        private static CultureInfo _cultureInfo = new CultureInfo("en-US");

        /// <summary>
        /// Convert numeric value d to a string with convenient formatting.
        /// </summary>
        public static string FormatNumber(double? d)
        {
            string s;

            if (d.HasValue)
            {
                double dAbs = Math.Abs(d.Value);

                if (dAbs < 1e-2)
                {
                    s = String.Format(_cultureInfo, "{0:0.##e0}", d.Value);
                }
                else if (dAbs > 1e+6)
                {
                    s = String.Format(_cultureInfo, "{0:0.##e+0}", d.Value);
                }
                else if (dAbs > 1e+3)
                {
                    s = String.Format(_cultureInfo, "{0:0.}", d.Value);
                }
                else
                {
                    s = String.Format(_cultureInfo, "{0:0.##}", d.Value);
                }
            }
            else
            {
                s = "--";
            }

            return s;
        }

        /// <summary>
        /// Convert numeric value d to a percentage string, e.g. d==0.253212
        /// returns string "25.32%"
        /// </summary>
        public static string FormatPercent(double? d)
        {
            return String.Format(_cultureInfo, "{0:0.##}%", d*100);
        }
    }
}