Skip to content

Commit 660d38b

Browse files
authored
Merge pull request Blankj#1320 from faramarzaf/master
TemperatureUtils class added to the lib-subutil
2 parents 20ff521 + 2d4fec4 commit 660d38b

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.blankj.subutil.util;
2+
3+
/**
4+
* Create by Faramarz Afzali on 2020/9/5
5+
* <p>
6+
* This class is intended for converting temperatures into different units.
7+
* C refers to the Celsius unit
8+
* F refers to the Fahrenheit unit
9+
* K refers to the Kelvin unit
10+
*/
11+
12+
public final class TemperatureUtils {
13+
14+
15+
public static float cToF(float temp) {
16+
return (temp * 9) / 5 + 32;
17+
}
18+
19+
public static float cToK(float temp) {
20+
return temp + 273.15f;
21+
}
22+
23+
24+
public static float fToC(float temp) {
25+
return (temp - 32) * 5 / 9;
26+
}
27+
28+
public static float fToK(float temp) {
29+
return temp + 255.3722222222f;
30+
}
31+
32+
33+
public static float kToC(float temp) {
34+
return temp - 273.15f;
35+
}
36+
37+
public static float kToF(float temp) {
38+
return temp - 459.67f;
39+
}
40+
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.blankj.subutil.util;
2+
3+
4+
import org.junit.Assert;
5+
import org.junit.Test;
6+
import org.junit.runner.RunWith;
7+
import org.junit.runners.JUnit4;
8+
9+
/**
10+
* Create by Faramarz Afzali on 2020/9/5
11+
*/
12+
13+
14+
@RunWith(JUnit4.class)
15+
public class TestTempConversion {
16+
17+
private float delta = 1e-15f;
18+
19+
@Test
20+
public void testCToF() {
21+
Assert.assertEquals(32f, TemperatureUtils.cToF(0f), delta);
22+
}
23+
24+
@Test
25+
public void testCToK() {
26+
Assert.assertEquals(273.15f, TemperatureUtils.cToK(0f), delta);
27+
}
28+
29+
30+
@Test
31+
public void testFToC() {
32+
Assert.assertEquals(-17.777779f, TemperatureUtils.fToC(0f), delta);
33+
}
34+
35+
@Test
36+
public void testFToK() {
37+
Assert.assertEquals(255.3722222222f, TemperatureUtils.fToK(0f), delta);
38+
}
39+
40+
41+
@Test
42+
public void testKToC() {
43+
Assert.assertEquals(-273.15f, TemperatureUtils.kToC(0f), delta);
44+
}
45+
46+
@Test
47+
public void testKToF() {
48+
Assert.assertEquals(-459.67f, TemperatureUtils.kToF(0f), delta);
49+
}
50+
51+
}

0 commit comments

Comments
 (0)