-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use custom logic for Rounding Down Operation
* add custom logic for truncating extra decimal digits * improve CurrencyRounderTest with Parameterized Tests * fix spotless errors * clean up code
- Loading branch information
Showing
5 changed files
with
160 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
library/src/main/java/com/cottacush/android/currencyedittext/CurrencyRounder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/** | ||
* Copyright (c) 2019 Cotta & Cush Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.cottacush.android.currencyedittext | ||
|
||
/** | ||
* Helper method to truncate extra decimal digits from numbers. | ||
* Was created because the previously used approach, [java.math.RoundingMode.DOWN] approach | ||
* didn't work correctly for some devices. | ||
* | ||
* @param number the original number to format | ||
* @param maxDecimalDigits the maximum number of decimal digits permitted | ||
* @param decimalSeparator the decimal separator of the currently selected locale | ||
* @return a version of number that has a maximum of [maxDecimalDigits] decimal digits. | ||
* e.g. | ||
* - 14.333 with 2 max decimal digits return 14.33 | ||
* - 19.2 with 2 max decimal digits return 19.2 | ||
*/ | ||
fun truncateNumberToMaxDecimalDigits( | ||
number: String, | ||
maxDecimalDigits: Int, | ||
decimalSeparator: Char | ||
): String { | ||
// Split number into whole and decimal part | ||
val arr = number | ||
.split(decimalSeparator) | ||
.toMutableList() | ||
|
||
// We should have exactly 2 elements in our string; | ||
// the whole part and the decimal part | ||
if (arr.size != 2) { | ||
return number | ||
} | ||
|
||
// Take the first n (or shorter) from the decimal digits. | ||
arr[1] = arr[1].take(maxDecimalDigits) | ||
|
||
return arr.joinToString(separator = decimalSeparator.toString()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
library/src/test/java/com/cottacush/android/currencyedittext/CurrencyRounderTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/** | ||
* Copyright (c) 2019 Cotta & Cush Limited. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.cottacush.android.currencyedittext | ||
|
||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.Parameterized | ||
|
||
@RunWith(Parameterized::class) | ||
class CurrencyRounderTest( | ||
private val current: String, | ||
private val expected: String, | ||
private val decimalDigits: Int, | ||
private val decimalSeparator: Char | ||
) { | ||
|
||
companion object { | ||
private const val POINT_DECIMAL_SEPARATOR = '.' | ||
private const val COMMA_DECIMAL_SEPARATOR = ',' | ||
private const val TWO = 2 | ||
private const val THREE = 3 | ||
|
||
private val emptyStringTestCase = arrayOf("", "", TWO, POINT_DECIMAL_SEPARATOR) | ||
private val extraDecimalTestCase1 = arrayOf("223.55644234234", "223.55", TWO, POINT_DECIMAL_SEPARATOR) | ||
private val extraDecimalTestCaseThreeDpVersion = arrayOf("223.55644234234", "223.556", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val extraDecimalTestCase2 = arrayOf("334,242,203.4234234", "334,242,203.423", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val extraDecimalTestCaseWhiteSpaceGrouping = arrayOf("334 242 203.4234234", "334 242 203.423", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val extraDecimalTestCaseCommaDecimal = arrayOf("334.242.203,4234234", "334.242.203,42", TWO, COMMA_DECIMAL_SEPARATOR) | ||
private val emptyWholeTestCase = arrayOf(".4234234", ".42", TWO, POINT_DECIMAL_SEPARATOR) | ||
private val emptyDecimalTestCase = arrayOf("4,234,234.", "4,234,234.", TWO, POINT_DECIMAL_SEPARATOR) | ||
private val shorterDecimalTestCase = arrayOf("23.45", "23.45", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val shorterDecimalTestCase2 = arrayOf("23.4", "23.4", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val exactDecimalTestCase = arrayOf("515.809", "515.809", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val noDecimalTestCase = arrayOf("5 809", "5 809", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val noDecimalTestCase2 = arrayOf("5", "5", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val noDecimalTestCase3 = arrayOf("5,452,635,242,242,423,434,333", "5,452,635,242,242,423,434,333", THREE, POINT_DECIMAL_SEPARATOR) | ||
private val multipleDecimalTestCase = arrayOf("343,432,242,342", "343,432,242,342", TWO, COMMA_DECIMAL_SEPARATOR) | ||
|
||
@JvmStatic | ||
@Parameterized.Parameters | ||
fun data(): Iterable<Array<Any>> = listOf( | ||
emptyStringTestCase, | ||
extraDecimalTestCase1, | ||
extraDecimalTestCaseThreeDpVersion, | ||
extraDecimalTestCase2, | ||
extraDecimalTestCaseWhiteSpaceGrouping, | ||
extraDecimalTestCaseCommaDecimal, | ||
emptyWholeTestCase, | ||
emptyDecimalTestCase, | ||
shorterDecimalTestCase, | ||
shorterDecimalTestCase2, | ||
exactDecimalTestCase, | ||
noDecimalTestCase, | ||
noDecimalTestCase2, | ||
noDecimalTestCase3, | ||
multipleDecimalTestCase | ||
) | ||
} | ||
|
||
@Test | ||
fun `should return expected value for set of valid inputs`() { | ||
Assert.assertEquals( | ||
expected, | ||
truncateNumberToMaxDecimalDigits(current, decimalDigits, decimalSeparator) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters