Skip to content

Commit e246fad

Browse files
committed
Fix rounding issue in PostDayViewsMapper
1 parent 2fd9406 commit e246fad

2 files changed

Lines changed: 10 additions & 7 deletions

File tree

WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/detail/PostDayViewsMapper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
1313
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
1414
import org.wordpress.android.util.text.PercentFormatter
1515
import org.wordpress.android.viewmodel.ResourceProvider
16+
import java.math.RoundingMode.HALF_UP
1617
import javax.inject.Inject
1718

1819
class PostDayViewsMapper
@@ -67,7 +68,7 @@ class PostDayViewsMapper
6768
0 -> ""
6869
else -> {
6970
val percentageValue = difference.toFloat() / previousValue
70-
percentFormatter.format(percentageValue)
71+
percentFormatter.format(value = percentageValue, rounding = HALF_UP)
7172
}
7273
}
7374
val formattedDifference = mapIntToString(difference, isFormattedNumber)

WordPress/src/test/java/org/wordpress/android/ui/stats/refresh/lists/detail/PostDayViewsMapperTest.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
1717
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
1818
import org.wordpress.android.util.text.PercentFormatter
1919
import org.wordpress.android.viewmodel.ResourceProvider
20+
import java.math.RoundingMode
21+
import java.math.RoundingMode.HALF_UP
2022

2123
class PostDayViewsMapperTest : BaseUnitTest() {
2224
@Mock lateinit var statsDateFormatter: StatsDateFormatter
@@ -57,7 +59,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
5759

5860
@Test
5961
fun `builds title with positive difference`() {
60-
whenever(percentFormatter.format(3.0F)).thenReturn("300")
62+
whenever(percentFormatter.format(value = 3.0F, rounding = RoundingMode.HALF_UP)).thenReturn("300")
6163
val previousCount = 5
6264
val previousItem = selectedItem.copy(count = previousCount)
6365
val positiveLabel = "+15 (300%)"
@@ -92,7 +94,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
9294

9395
@Test
9496
fun `builds title with negative difference`() {
95-
whenever(percentFormatter.format(-0.33333334F)).thenReturn("-33")
97+
whenever(percentFormatter.format(value = -0.33333334F, rounding = HALF_UP)).thenReturn("-33")
9698
val previousCount = 30
9799
val previousItem = selectedItem.copy(count = previousCount)
98100
val negativeLabel = "-10 (-33%)"
@@ -109,7 +111,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
109111

110112
@Test
111113
fun `builds title with max negative difference`() {
112-
whenever(percentFormatter.format(-1F)).thenReturn("-100")
114+
whenever(percentFormatter.format(value = -1F, rounding = HALF_UP)).thenReturn("-100")
113115
val newCount = 0
114116
val newItem = selectedItem.copy(count = newCount)
115117
val negativeLabel = "-20 (-100%)"
@@ -150,7 +152,7 @@ class PostDayViewsMapperTest : BaseUnitTest() {
150152

151153
@Test
152154
fun `builds title with negative difference for the last item`() {
153-
whenever(percentFormatter.format(-0.33333334F)).thenReturn("-33")
155+
whenever(percentFormatter.format(value = -0.33333334F, rounding = HALF_UP)).thenReturn("-33")
154156
val previousCount = 30
155157
val previousItem = selectedItem.copy(count = previousCount)
156158
val negativeLabel = "-10 (-33%)"
@@ -167,12 +169,12 @@ class PostDayViewsMapperTest : BaseUnitTest() {
167169

168170
@Test
169171
fun `should call PercentFormatter when builds title`() {
170-
whenever(percentFormatter.format(3.0F)).thenReturn("3%")
172+
whenever(percentFormatter.format(value = 3.0F, rounding = HALF_UP)).thenReturn("3%")
171173
val previousCount = 5
172174
val previousItem = selectedItem.copy(count = previousCount)
173175
mapper.buildTitle(selectedItem, previousItem, false)
174176

175177
// buildChange is called twice: for change and unformattedChange
176-
verify(percentFormatter, times(2)).format(3.0F)
178+
verify(percentFormatter, times(2)).format(value = 3.0F, rounding = HALF_UP)
177179
}
178180
}

0 commit comments

Comments
 (0)