File size: 2,865 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/// ------------------------------------------------------
/// 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/
/// ------------------------------------------------------

namespace SwarmOps
{
    public static partial class Tools
    {
        /// <summary>
        /// Convert an array of System.double values to a string.
        /// </summary>
        /// <param name="x">Array of values to be converted to string.</param>
        public static string ArrayToString(double[] x)
        {
            return ArrayToString(x, 4);
        }

        /// <summary>
        /// Convert an array of System.double values to a string.
        /// </summary>
        /// <param name="x">Array of values to be converted to string.</param>
        /// <param name="digits">Number of digits for each value.</param>
        /// <returns></returns>
        public static string ArrayToString(double[] x, int digits)
        {
            string s = "{ ";

            if (x.Length>0)
            {
                s += NumberToString(x[0], digits);

                for (int i = 1; i < x.Length; i++)
                {
                    s += ", " + NumberToString(x[i], digits);
                }
            }

            s += " }";

            return s;
        }

        /// <summary>
        /// Convert an array of System.double values to a string. Not formatted like a C# array.
        /// </summary>
        /// <param name="x">Array of values to be converted to string.</param>
        public static string ArrayToStringRaw(double[] x)
        {
            return ArrayToStringRaw(x, 4);
        }

        /// <summary>
        /// Convert an array of System.double values to a string. Not formattet like a C# array.
        /// </summary>
        /// <param name="x">Array of values to be converted to string.</param>
        /// <param name="digits">Number of digits for each value.</param>
        /// <returns></returns>
        public static string ArrayToStringRaw(double[] x, int digits)
        {
            string s = "";

            for (int i = 0; i < x.Length; i++)
            {
                s += NumberToString(x[i], digits) + " ";
            }

            return s;
        }

        /// <summary>
        /// Convert a double-value to a string, formatted for array output.
        /// </summary>
        /// <param name="x">Value to convert.</param>
        /// <param name="digits">Number of digits.</param>
        public static string NumberToString(double x, int digits)
        {
            return System.Math.Round(x, digits).ToString(_cultureInfo);
        }
    }
}