|
7 | 7 |
|
8 | 8 | package conversion |
9 | 9 |
|
| 10 | +// Data size constants |
| 11 | +const ( |
| 12 | + KiloByte = 1_024 |
| 13 | + MegaByte = 1_048_576 |
| 14 | + GigaByte = 1_073_741_824 |
| 15 | +) |
| 16 | + |
| 17 | +// Time constants |
| 18 | +const ( |
| 19 | + SecondsPerMinute = 60 |
| 20 | + MinutesPerHour = 60 |
| 21 | + HoursPerDay = 24 |
| 22 | +) |
| 23 | + |
| 24 | +// Temperature constants |
| 25 | +const ( |
| 26 | + CelsiusFahrenheitOffset = 32 |
| 27 | + CelsiusToKelvinOffset = 273.15 |
| 28 | + |
| 29 | + FahrenheitToKelvinOffset = 459.67 |
| 30 | + |
| 31 | + CelsiusToFahrenheitFactor = 9.0 / 5.0 |
| 32 | + FahrenheitToCelsiusFactor = 5.0 / 9.0 |
| 33 | + |
| 34 | + roundingDecimalPlaces = 2 |
| 35 | +) |
| 36 | + |
10 | 37 | // Data size conversion functions |
11 | 38 |
|
12 | 39 | // BytesToKB converts bytes to kilobytes. |
13 | 40 | // It takes an integer value of bytes and returns the equivalent in kilobytes as a float64. |
14 | 41 | func BytesToKB(b int64) float64 { |
15 | | - return float64(b) / 1024 |
| 42 | + return float64(b) / KiloByte |
16 | 43 | } |
17 | 44 |
|
18 | 45 | // KBToBytes converts kilobytes to bytes. |
19 | 46 | // It takes a float64 value of kilobytes and returns the equivalent in bytes as an int64. |
20 | 47 | func KBToBytes(kb float64) int64 { |
21 | | - return int64(kb * 1024) |
| 48 | + return int64(kb * KiloByte) |
22 | 49 | } |
23 | 50 |
|
24 | 51 | // BytesToMB converts bytes to megabytes. |
25 | | -// It takes an interger value of bytes and return a float64 value of megabytes |
| 52 | +// It takes an integer value of bytes and returns the equivalent in megabytes as a float64. |
26 | 53 | func BytesToMB(b int64) float64 { |
27 | | - return float64(b) / 1048576 |
| 54 | + return float64(b) / MegaByte |
28 | 55 | } |
29 | 56 |
|
30 | 57 | // MBToBytes converts megabytes to bytes. |
31 | 58 | // It takes a float64 value of megabytes and returns the equivalent in bytes as an int64. |
32 | 59 | func MBToBytes(mb float64) int64 { |
33 | | - return int64(mb * 1048576) |
| 60 | + return int64(mb * MegaByte) |
34 | 61 | } |
35 | 62 |
|
36 | 63 | // BytesToGB converts bytes to gigabytes |
37 | | -// It takes an integer value of bytes and return the equivalent as a float64 |
| 64 | +// It takes an integer value of bytes and returns the equivalent in gigabytes as a float64. |
38 | 65 | func BytesToGB(b int64) float64 { |
39 | | - return float64(b) / 1073741824 |
| 66 | + return float64(b) / GigaByte |
40 | 67 | } |
41 | 68 |
|
42 | 69 | // GBToBytes converts gigabytes to bytes. |
43 | 70 | // It takes a float64 value of gigabytes and returns the equivalent in bytes as an int64. |
44 | 71 | func GBToBytes(gb float64) int64 { |
45 | | - return int64(gb * 1073741824) |
| 72 | + return int64(gb * GigaByte) |
46 | 73 | } |
47 | 74 |
|
48 | 75 | // Time conversion functions |
49 | 76 |
|
50 | 77 | // SecondsToMinutes converts seconds to minutes. |
51 | 78 | // It takes an integer value of seconds and returns the equivalent in minutes as a float64. |
52 | 79 | func SecondsToMinutes(s int64) float64 { |
53 | | - return float64(s) / 60 |
| 80 | + return float64(s) / SecondsPerMinute |
54 | 81 | } |
55 | 82 |
|
56 | 83 | // MinutesToSeconds converts minutes to seconds. |
57 | 84 | // It takes an integer value of minutes and returns the equivalent in seconds as an int64. |
58 | 85 | func MinutesToSeconds(m int64) int64 { |
59 | | - return m * 60 |
| 86 | + return m * SecondsPerMinute |
60 | 87 | } |
61 | 88 |
|
62 | 89 | // MinutesToHours converts minutes to hours. |
63 | 90 | // It takes an integer value of minutes and returns the equivalent in hours as a float64. |
64 | 91 | func MinutesToHours(m int64) float64 { |
65 | | - return float64(m) / 60 |
| 92 | + return float64(m) / MinutesPerHour |
66 | 93 | } |
67 | 94 |
|
68 | 95 | // HoursToMinutes converts hours to minutes. |
69 | 96 | // It takes an integer value of hours and returns the equivalent in minutes as an int64. |
70 | 97 | func HoursToMinutes(h int64) int64 { |
71 | | - return h * 60 |
| 98 | + return h * MinutesPerHour |
72 | 99 | } |
73 | 100 |
|
74 | 101 | // HoursToDays converts hours to days. |
75 | 102 | // It takes an integer value of hours and returns the equivalent in days as a float64. |
76 | 103 | func HoursToDays(h int64) float64 { |
77 | | - return float64(h) / 24 |
| 104 | + return float64(h) / HoursPerDay |
78 | 105 | } |
79 | 106 |
|
80 | 107 | // DaysToHours converts days to hours. |
81 | | -// It takes an integer value of days and returns the equivalent in days as an int64. |
| 108 | +// It takes an integer value of days and returns the equivalent in hours as an int64. |
82 | 109 | func DaysToHours(d int64) int64 { |
83 | | - return d * 24 |
| 110 | + return d * HoursPerDay |
84 | 111 | } |
85 | 112 |
|
86 | 113 | // Temperature conversion functions |
87 | 114 |
|
88 | 115 | // CelsiusToFahrenheit converts Celsius to Fahrenheit. |
89 | 116 | // It takes a float64 value of Celsius and returns the equivalent in Fahrenheit as a float64. |
90 | 117 | func CelsiusToFahrenheit(c float64) float64 { |
91 | | - return RoundToTwoDecimals((c * 9 / 5) + 32) |
| 118 | + output := (c * CelsiusToFahrenheitFactor) + CelsiusFahrenheitOffset |
| 119 | + |
| 120 | + return roundToDecimalPlaces(output, roundingDecimalPlaces) |
92 | 121 | } |
93 | 122 |
|
94 | 123 | // FahrenheitToCelsius converts Fahrenheit to Celsius. |
95 | 124 | // It takes a float64 value of Fahrenheit and returns the equivalent in Celsius as a float64. |
96 | 125 | func FahrenheitToCelsius(f float64) float64 { |
97 | | - return RoundToTwoDecimals((f - 32) * 5 / 9) |
| 126 | + output := (f - CelsiusFahrenheitOffset) * FahrenheitToCelsiusFactor |
| 127 | + |
| 128 | + return roundToDecimalPlaces(output, roundingDecimalPlaces) |
98 | 129 | } |
99 | 130 |
|
100 | 131 | // CelsiusToKelvin converts Celsius to Kelvin. |
101 | 132 | // It takes a float64 value of Celsius and returns the equivalent in Kelvin as a float64. |
102 | 133 | func CelsiusToKelvin(c float64) float64 { |
103 | | - return RoundToTwoDecimals(c + 273.15) |
| 134 | + output := c + CelsiusToKelvinOffset |
| 135 | + |
| 136 | + return roundToDecimalPlaces(output, roundingDecimalPlaces) |
104 | 137 | } |
105 | 138 |
|
106 | 139 | // KelvinToCelsius converts Kelvin to Celsius. |
107 | 140 | // It takes a float64 value of Kelvin and returns the equivalent in Celsius as a float64. |
108 | 141 | func KelvinToCelsius(k float64) float64 { |
109 | | - return RoundToTwoDecimals(k - 273.15) |
| 142 | + output := k - CelsiusToKelvinOffset |
| 143 | + |
| 144 | + return roundToDecimalPlaces(output, roundingDecimalPlaces) |
110 | 145 | } |
111 | 146 |
|
112 | 147 | // FahrenheitToKelvin converts Fahrenheit to Kelvin. |
113 | 148 | // It takes a float64 value of Fahrenheit and returns the equivalent in Kelvin as a float64. |
114 | 149 | func FahrenheitToKelvin(f float64) float64 { |
115 | | - return RoundToTwoDecimals((f + 459.67) * 5 / 9) |
| 150 | + output := (f + FahrenheitToKelvinOffset) * FahrenheitToCelsiusFactor |
| 151 | + |
| 152 | + return roundToDecimalPlaces(output, roundingDecimalPlaces) |
116 | 153 | } |
117 | 154 |
|
118 | 155 | // KelvinToFahrenheit converts Kelvin to Fahrenheit. |
119 | 156 | // It takes a float64 value of Kelvin and returns the equivalent in Fahrenheit as a float64. |
120 | 157 | func KelvinToFahrenheit(k float64) float64 { |
121 | | - return RoundToTwoDecimals((k-273.15)*9.0/5.0 + 32) |
| 158 | + output := (k-CelsiusToKelvinOffset)*CelsiusToFahrenheitFactor + CelsiusFahrenheitOffset |
| 159 | + |
| 160 | + return roundToDecimalPlaces(output, roundingDecimalPlaces) |
122 | 161 | } |
0 commit comments