File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed
core-kotlin-modules/core-kotlin-numbers-2/src/test/kotlin/com/baeldung/intToFloat Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
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 `convert int to float using toFloat() method` () {
13
+ val intValue = 12
14
+ val floatValue = intValue.toFloat()
15
+
16
+ assertEquals(12.0f , floatValue)
17
+ }
18
+
19
+ @Test
20
+ fun `convert int to float using arithmetic` () {
21
+ val intValue = 12
22
+ val floatValue = intValue + 0.0f
23
+
24
+ assertEquals(12.0f , floatValue)
25
+ }
26
+
27
+ @Test
28
+ fun `convert int to float using NumberFormat class` () {
29
+ val intValue = 12
30
+ val floatValue = convertIntToFloatUsingNumberFormat(intValue)
31
+ assertEquals(12.0f , floatValue)
32
+ }
33
+
34
+ @Test
35
+ fun `convert int to float using BigDecimal` () {
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
+ }
You can’t perform that action at this time.
0 commit comments