Esempio n. 1
0
        public static string ToString(double[][] counts, int cellSize, Object[] rowLabels, Object[] colLabels, NumberFormatInfo nf, bool printTotals)
        {
            if (counts == null)
                return null;
            double[] rowTotals = new double[counts.Length];
            double[] colTotals = new double[counts[0].Length];
            double total = 0.0;
            for (int i = 0; i < counts.Length; i++)
            {
                for (int j = 0; j < counts[i].Length; j++)
                {
                    rowTotals[i] += counts[i][j];
                    colTotals[j] += counts[i][j];
                    total += counts[i][j];
                }
            }

            StringBuilder result = new StringBuilder();
            if (colLabels != null)
            {
                result.Append(StringUtils.PadLeft(@"", cellSize));
                for (int j = 0; j < counts[0].Length; j++)
                {
                    string s = colLabels[j].ToString();
                    if (s.Length > cellSize - 1)
                    {
                        s = s.Substring(0, cellSize - 1);
                    }

                    s = StringUtils.PadLeft(s, cellSize);
                    result.Append(s);
                }

                if (printTotals)
                {
                    result.Append(StringUtils.PadLeftOrTrim(@"Total", cellSize));
                }

                result.Append('\n');
            }

            for (int i = 0; i < counts.Length; i++)
            {
                if (rowLabels != null)
                {
                    string s = rowLabels[i].ToString();
                    s = StringUtils.PadOrTrim(s, cellSize);
                    result.Append(s);
                }

                for (int j = 0; j < counts[i].Length; j++)
                {
                    result.Append(StringUtils.PadLeft((counts[i][j]).ToString(nf), cellSize));
                }

                if (printTotals)
                {
                    result.Append(StringUtils.PadLeft((rowTotals[i]).ToString(nf), cellSize));
                }

                result.Append('\n');
            }

            if (printTotals)
            {
                result.Append(StringUtils.Pad(@"Total", cellSize));
                foreach (double colTotal in colTotals)
                {
                    result.Append(StringUtils.PadLeft(nf.Format(colTotal), cellSize));
                }

                result.Append(StringUtils.PadLeft(nf.Format(total), cellSize));
            }

            return result.ToString();
        }