0

My issue is I currently have a value that is £17.40, this is then converted into Euros which becomes €20.01. when selecting the button which which would round up the amount it reads €20.00 but should read €30.00 (with €30 being the next whole amount to pay with).

private fun getNextLogicalCashAmount(currentAmount: Double = getBasket()?.getAmountRemaining() ?: 0.00): Double
{

    var amount = ((Math.ceil(currentAmount) / 10.00).toInt() * 10).toDouble()
    if (amount <= currentAmount )
    {
        amount += 10


    }
    return amount
}

private fun setConvertedAmount(amount: Double)
{
    val nativeFormatter = LocalityHelper.getNumberFormatter()
    converted_amount?.visibility = if (amount > 0.00) View.VISIBLE else View.INVISIBLE
    converted_amount?.text = getString(R.string.same_as_amount, nativeFormatter.format(convertBack(amount)))
}

/**
 * Return the custom amount entered into the EditText as a Double.
 */
private fun getCustomAmount(): Double
{
    return try
    {
        custom_amount?.text.toString().toDouble()
    }
    catch (e: Exception)
    {
        0.00
    }
}

private fun convert(amount: Double): Double = providers.getCurrencyConverter().convert(
        amount,
        providers.getMarketOptions().getCurrencyCode(),
        providers.getStoreOptions().getForeignCurrencyCode()!!
)

private fun convertBack(amount: Double): Double = providers.getCurrencyConverter().convert(
        amount,
        providers.getStoreOptions().getForeignCurrencyCode()!!,
        providers.getMarketOptions().getCurrencyCode()
)

private fun takePayment(amount: Double, foreignAmount: Double)
{
    if (amount > 0.00 && foreignAmount > 0.00)
    {
        CashDrawerDialog().setListener { addPayment(amount, foreignAmount, it) }.show(this)
    }
    else
    {
        Toast.makeText(context, R.string.enter_valid_amount, Toast.LENGTH_SHORT).show()
    }
}

It seems I am still picking up the value from the £. So when the £ reaches £20, the € will change to €30.00, but when the the foreign currency is at €20.1 it will still show as €20.00.

Oliver
  • 926
  • 2
  • 12
  • 31
Brummerly
  • 90
  • 1
  • 11
  • 1
    Not the answer to your question as such, but you will remove a lof ot ambiguity by using the Kotlin Long type instead of Double to work with currency values. While Double seems like a logical fit, it can introduce all sorts of problems when doing currency arithmetic. Using a Long means that you must work in hundredths of the currency unit (pence, cents, centimes etc) and then you must move the decimal point two to the left when displaying. – Rich Feb 08 '19 at 13:07
  • Your function `getNextLogicalCashAmount()` works fine, when passed 20.1 returns 30.0 – forpas Feb 08 '19 at 13:17
  • See this question: https://stackoverflow.com/questions/3730019/.  And https://softwareengineering.stackexchange.com/questions/228584 and https://dzone.com/articles/never-use-float-and-double-for-monetary-calculatio and https://floating-point-gui.de/ and http://wiki.c2.com/?FloatingPointCurrency and http://blog.plataformatec.com.br/2014/09/floating-point-and-currency/ and…  TLDR: **DO NOT Store Currency Values In Floating-Point!**  Use `BigDecimal` or `Long` instead. – gidds Feb 10 '19 at 12:24

0 Answers0