Skip to content

Commit 2cc7f48

Browse files
committed
Added unit test
1 parent b5176a4 commit 2cc7f48

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.intToFloat
2+
3+
import org.junit.jupiter.api.Assertions.assertEquals
4+
import org.junit.jupiter.api.Test
5+
import java.math.BigDecimal
6+
import java.text.NumberFormat
7+
import java.util.*
8+
9+
class IntToFloatUnitTest {
10+
11+
@Test
12+
fun testConvertIntToFloatUsingToFloat() {
13+
val intValue = 12
14+
val floatValue = intValue.toFloat()
15+
16+
assertEquals(12.0f, floatValue)
17+
}
18+
19+
@Test
20+
fun testConvertIntToFloatUsingArithmetic() {
21+
val intValue = 12
22+
val floatValue = intValue + 0.0f
23+
24+
assertEquals(12.0f, floatValue)
25+
}
26+
27+
@Test
28+
fun testConvertIntToFloatUsingNumberFormat() {
29+
val intValue = 12
30+
val floatValue = convertIntToFloatUsingNumberFormat(intValue)
31+
assertEquals(12.0f, floatValue)
32+
}
33+
34+
@Test
35+
fun testConvertIntToFloatUsingBigDecimal() {
36+
val intValue = 12
37+
val floatValue = BigDecimal(intValue).toFloat()
38+
39+
assertEquals(12.0f, floatValue)
40+
}
41+
42+
}
43+
44+
fun convertIntToFloatUsingNumberFormat(value: Int): Float {
45+
val numberFormat = NumberFormat.getInstance(Locale.US)
46+
return numberFormat.parse(value.toString()).toFloat()
47+
}

0 commit comments

Comments
 (0)