forked from codinguser/gnucash-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid losing precision when the user enters an exchange rate.
We were storing the rate as originAmount/convertedAmount, which was limited to the precision of the currencies. Now we store directly the entered rate. Fixes codinguser#479
- Loading branch information
Showing
3 changed files
with
114 additions
and
18 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
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
54 changes: 54 additions & 0 deletions
54
app/src/test/java/org/gnucash/android/test/unit/model/PriceTest.java
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,54 @@ | ||
/* | ||
* Copyright (c) 2016 Àlex Magaz Graça <rivaldi8@gmail.com> | ||
* | ||
* 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 org.gnucash.android.test.unit.model; | ||
|
||
import org.gnucash.android.model.Price; | ||
import org.junit.Test; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Locale; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
|
||
public class PriceTest { | ||
@Test | ||
public void creatingFromExchangeRate_ShouldGetPrecisionRight() { | ||
Locale.setDefault(Locale.US); | ||
|
||
String exchangeRateString = "0.123456"; | ||
BigDecimal exchangeRate = new BigDecimal(exchangeRateString); | ||
Price price = new Price("commodity1UID", "commodity2UID", exchangeRate); | ||
assertThat(price.toString()).isEqualTo(exchangeRateString); | ||
|
||
// ensure we don't get more decimal places than needed (0.123000) | ||
exchangeRateString = "0.123"; | ||
exchangeRate = new BigDecimal(exchangeRateString); | ||
price = new Price("commodity1UID", "commodity2UID", exchangeRate); | ||
assertThat(price.toString()).isEqualTo(exchangeRateString); | ||
} | ||
|
||
@Test | ||
public void toString_shouldUseDefaultLocale() { | ||
Locale.setDefault(Locale.GERMANY); | ||
|
||
String exchangeRateString = "1.234"; | ||
BigDecimal exchangeRate = new BigDecimal(exchangeRateString); | ||
Price price = new Price("commodity1UID", "commodity2UID", exchangeRate); | ||
assertThat(price.toString()).isEqualTo("1,234"); | ||
} | ||
} |