Merge branch 'seybur-master' #43
[VesselSimulator.git] / KerbalEngineer / Helpers / Units.cs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 
//     Kerbal Engineer Redux
// 
//     Copyright (C) 2014 CYBUTEK
// 
//     This program is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
 
#region Using Directives
 
using System;
 
#endregion
 
namespace KerbalEngineer.Helpers
{
    public static class Units
    {
        #region Methods: public
 
        public static string Concat(double value1, double value2, int decimals = 1)
        {
            return value1.ToString("F" + decimals) + " / " + value2.ToString("F" + decimals);
        }
 
        public static string Concat(double value1, double value2, double value3, int decimals = 1)
        {
            return value1.ToString("F" + decimals) + " / " + value2.ToString("F" + decimals) + " / " + value3.ToString("F" + decimals);
        }
 
        public static string ToAcceleration(double value, int decimals = 2)
        {
            return value.ToString("N" + decimals) + "m/s²";
        }
 
        public static string ToAcceleration(double value1, double value2, int decimals = 2)
        {
            return value1.ToString("N" + decimals) + " / " + value2.ToString("N" + decimals) + "m/s²";
        }
 
        public static string ToAngle(double value, int decimals = 5)
        {
            return value.ToString("F" + decimals) + "°";
        }
 
        public static string ToDistance(double value, int decimals = 1)
        {
            if (Math.Abs(value) < 1000000.0)
            {
                if (Math.Abs(value) >= 10.0)
                {
                    return value.ToString("N" + decimals) + "m";
                }
 
                value *= 100.0;
                if (Math.Abs(value) >= 100.0)
                {
                    return value.ToString("N" + decimals) + "cm";
                }
 
                value *= 10.0;
                return value.ToString("N" + decimals) + "mm";
            }
 
            value /= 1000.0;
            if (Math.Abs(value) < 1000000.0)
            {
                return value.ToString("N" + decimals) + "km";
            }
 
            value /= 1000.0;
            return value.ToString("N" + decimals) + "Mm";
        }
 
        public static string ToTorque(double value)
        {
            return value.ToString((value < 100.0) ? (Math.Abs(value) < Double.Epsilon) ? "N0" : "N1" : "N0") + "kNm";
        }
 
        public static string ToForce(double value)
        {
            return value.ToString((value < 100000.0) ? (value < 10000.0) ? (value < 100.0) ? (Math.Abs(value) < Double.Epsilon) ? "N0" : "N3" : "N2" : "N1" : "N0") + "kN";
        }
 
        public static string ToForce(double value1, double value2)
        {
            var format1 = (value1 < 100000.0) ? (value1 < 10000.0) ? (value1 < 100.0) ? (Math.Abs(value1) < Double.Epsilon) ? "N0" : "N3" : "N2" : "N1" : "N0";
            var format2 = (value2 < 100000.0) ? (value2 < 10000.0) ? (value2 < 100.0) ? (Math.Abs(value2) < Double.Epsilon) ? "N0" : "N3" : "N2" : "N1" : "N0";
            return value1.ToString(format1) + " / " + value2.ToString(format2) + "kN";
        }
 
        public static string ToMass(double value, int decimals = 0)
        {
            value *= 1000.0;
            return value.ToString("N" + decimals) + "kg";
        }
 
        public static string ToMass(double value1, double value2, int decimals = 0)
        {
            value1 *= 1000.0;
            value2 *= 1000.0;
            return value1.ToString("N" + decimals) + " / " + value2.ToString("N" + decimals) + "kg";
        }
 
        public static string ToPercent(double value, int decimals = 2)
        {
            value *= 100.0;
            return value.ToString("F" + decimals) + "%";
        }
 
        public static string ToRate(double value, int decimals = 1)
        {
            return value < 1.0 ? (value * 60.0).ToString("F" + decimals) + "/min" : value.ToString("F" + decimals) + "/sec";
        }
 
        public static string ToSpeed(double value, int decimals = 2)
        {
            if (Math.Abs(value) < 1.0)
            {
                return (value * 1000.0).ToString("N" + decimals) + "mm/s";
            }
            return value.ToString("N" + decimals) + "m/s";
        }
 
        public static string ToTime(double value)
        {
            return TimeFormatter.ConvertToString(value);
        }
 
        #endregion
    }
}