Skip to content

Commit 70c5e22

Browse files
conversion: Refactoring of conversion + tests (#184)
Added conversion constants and refactored tests Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com>
1 parent 3e5f5b2 commit 70c5e22

3 files changed

Lines changed: 142 additions & 125 deletions

File tree

conversion/conversion.go

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,116 +7,155 @@
77

88
package conversion
99

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+
1037
// Data size conversion functions
1138

1239
// BytesToKB converts bytes to kilobytes.
1340
// It takes an integer value of bytes and returns the equivalent in kilobytes as a float64.
1441
func BytesToKB(b int64) float64 {
15-
return float64(b) / 1024
42+
return float64(b) / KiloByte
1643
}
1744

1845
// KBToBytes converts kilobytes to bytes.
1946
// It takes a float64 value of kilobytes and returns the equivalent in bytes as an int64.
2047
func KBToBytes(kb float64) int64 {
21-
return int64(kb * 1024)
48+
return int64(kb * KiloByte)
2249
}
2350

2451
// 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.
2653
func BytesToMB(b int64) float64 {
27-
return float64(b) / 1048576
54+
return float64(b) / MegaByte
2855
}
2956

3057
// MBToBytes converts megabytes to bytes.
3158
// It takes a float64 value of megabytes and returns the equivalent in bytes as an int64.
3259
func MBToBytes(mb float64) int64 {
33-
return int64(mb * 1048576)
60+
return int64(mb * MegaByte)
3461
}
3562

3663
// 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.
3865
func BytesToGB(b int64) float64 {
39-
return float64(b) / 1073741824
66+
return float64(b) / GigaByte
4067
}
4168

4269
// GBToBytes converts gigabytes to bytes.
4370
// It takes a float64 value of gigabytes and returns the equivalent in bytes as an int64.
4471
func GBToBytes(gb float64) int64 {
45-
return int64(gb * 1073741824)
72+
return int64(gb * GigaByte)
4673
}
4774

4875
// Time conversion functions
4976

5077
// SecondsToMinutes converts seconds to minutes.
5178
// It takes an integer value of seconds and returns the equivalent in minutes as a float64.
5279
func SecondsToMinutes(s int64) float64 {
53-
return float64(s) / 60
80+
return float64(s) / SecondsPerMinute
5481
}
5582

5683
// MinutesToSeconds converts minutes to seconds.
5784
// It takes an integer value of minutes and returns the equivalent in seconds as an int64.
5885
func MinutesToSeconds(m int64) int64 {
59-
return m * 60
86+
return m * SecondsPerMinute
6087
}
6188

6289
// MinutesToHours converts minutes to hours.
6390
// It takes an integer value of minutes and returns the equivalent in hours as a float64.
6491
func MinutesToHours(m int64) float64 {
65-
return float64(m) / 60
92+
return float64(m) / MinutesPerHour
6693
}
6794

6895
// HoursToMinutes converts hours to minutes.
6996
// It takes an integer value of hours and returns the equivalent in minutes as an int64.
7097
func HoursToMinutes(h int64) int64 {
71-
return h * 60
98+
return h * MinutesPerHour
7299
}
73100

74101
// HoursToDays converts hours to days.
75102
// It takes an integer value of hours and returns the equivalent in days as a float64.
76103
func HoursToDays(h int64) float64 {
77-
return float64(h) / 24
104+
return float64(h) / HoursPerDay
78105
}
79106

80107
// 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.
82109
func DaysToHours(d int64) int64 {
83-
return d * 24
110+
return d * HoursPerDay
84111
}
85112

86113
// Temperature conversion functions
87114

88115
// CelsiusToFahrenheit converts Celsius to Fahrenheit.
89116
// It takes a float64 value of Celsius and returns the equivalent in Fahrenheit as a float64.
90117
func CelsiusToFahrenheit(c float64) float64 {
91-
return RoundToTwoDecimals((c * 9 / 5) + 32)
118+
output := (c * CelsiusToFahrenheitFactor) + CelsiusFahrenheitOffset
119+
120+
return roundToDecimalPlaces(output, roundingDecimalPlaces)
92121
}
93122

94123
// FahrenheitToCelsius converts Fahrenheit to Celsius.
95124
// It takes a float64 value of Fahrenheit and returns the equivalent in Celsius as a float64.
96125
func FahrenheitToCelsius(f float64) float64 {
97-
return RoundToTwoDecimals((f - 32) * 5 / 9)
126+
output := (f - CelsiusFahrenheitOffset) * FahrenheitToCelsiusFactor
127+
128+
return roundToDecimalPlaces(output, roundingDecimalPlaces)
98129
}
99130

100131
// CelsiusToKelvin converts Celsius to Kelvin.
101132
// It takes a float64 value of Celsius and returns the equivalent in Kelvin as a float64.
102133
func CelsiusToKelvin(c float64) float64 {
103-
return RoundToTwoDecimals(c + 273.15)
134+
output := c + CelsiusToKelvinOffset
135+
136+
return roundToDecimalPlaces(output, roundingDecimalPlaces)
104137
}
105138

106139
// KelvinToCelsius converts Kelvin to Celsius.
107140
// It takes a float64 value of Kelvin and returns the equivalent in Celsius as a float64.
108141
func KelvinToCelsius(k float64) float64 {
109-
return RoundToTwoDecimals(k - 273.15)
142+
output := k - CelsiusToKelvinOffset
143+
144+
return roundToDecimalPlaces(output, roundingDecimalPlaces)
110145
}
111146

112147
// FahrenheitToKelvin converts Fahrenheit to Kelvin.
113148
// It takes a float64 value of Fahrenheit and returns the equivalent in Kelvin as a float64.
114149
func FahrenheitToKelvin(f float64) float64 {
115-
return RoundToTwoDecimals((f + 459.67) * 5 / 9)
150+
output := (f + FahrenheitToKelvinOffset) * FahrenheitToCelsiusFactor
151+
152+
return roundToDecimalPlaces(output, roundingDecimalPlaces)
116153
}
117154

118155
// KelvinToFahrenheit converts Kelvin to Fahrenheit.
119156
// It takes a float64 value of Kelvin and returns the equivalent in Fahrenheit as a float64.
120157
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)
122161
}

0 commit comments

Comments
 (0)