-1

I have started the online course "The Complete Android N Developer Course " and am at a section where I have questions about my code and the community really isn't of much assistance.

I've constantly tried looking at questions online and asking questions o the course community Q&A.

public void convert(View view){
    editText dollarAmounteditText = (editText) findViewById(R.id.dollarAmounteditText);
    Double dollarAmountDouble = Double.parseDouble(dollarAmounteditText);
    Double poundAmount = dollarAmountDouble = 0.75;
    Toast.makeText(mainActivity.this,  "£" + String.format("%.2f", poundAmount ), Toast.LENGTH_SHORT).show();
    Log.i("amount" , dollarAmounteditText.getText().toString());
}

I also have been getting this error message whenever I try to run the emulator.

"M Emulator: emulator: ERROR: x86 emulation currently requires hardware acceleration!"

azurefrog
  • 10,785
  • 7
  • 42
  • 56

1 Answers1

1

Never use double or BigDecimal for currency; both are ridiculous.

You should always identify the appropriate fraction of the currency to be the unit and use a long to represent that. For example, if you are working with USD, then a penny (1/100 of a USD) may be good. If you are converting between currencies, then 1/10_000 of the base currency (for example, USD) may be reasonable. In the case of 1/10_000 of the base currency, in my example 10_000 would be the value to store for $1 (one USD). for Yen to USD you might want to use 1/10_000_000 USD as the 1 value (i.e. store 10_000_000 for $1).

Once you have a reasonable representation, do integer math for currency (a long holds integer values).

Run this to see why double and BigDecimal are bad.


import java.math.BigDecimal;

import org.junit.Test;

public class GoofballsUseDouble
{
    public void bigdecimalTest1()
    {
        final BigDecimal m1 = new BigDecimal(
            0.1d);
        final BigDecimal m2 = new BigDecimal(
            0.2d);
        final BigDecimal sum;

        sum = m1.add(m2);

        System.out.println("");
        System.out.println("bigdecimalTest1");
        System.out.println(" v1.a (0.1bd): " + m1);
        System.out.println(" v2.a (0.2bd): " + m2);
        System.out.println("sum.a (?): " + sum);
    }

    public void bigdecimalTest2()
    {
        final BigDecimal m1 = new BigDecimal(
            "0.1");
        final BigDecimal m2 = new BigDecimal(
            "0.2");
        final BigDecimal sum;

        sum = m1.add(m2);

        System.out.println("");
        System.out.println("bigdecimalTest2");
        System.out.println(" v1.a (0.1bds): " + m1);
        System.out.println(" v2.a (0.2bds): " + m2);
        System.out.println("sum.a (?): " + sum);
    }

    public void doubleTest()
    {
        final double m1 = 0.1d;
        final double m2 = 0.2d;
        final double sumM;

        sumM = m1 + m2;

        System.out.println("");
        System.out.println("doubleTest");
        System.out.println(" v1.a (0.1d): " + m1);
        System.out.println(" v2.a (0.2d): " + m2);
        System.out.println("sum.a (?): " + sumM);

        System.out.printf(" v1.b (0.1d): %f%n",
            m1);
        System.out.printf(" v2.b (0.2d): %f%n",
            m2);

        if (.3d == sumM)
        {
            System.out.println("it r equalz");
        }
        else
        {
            System.out.println("double is not exact, of course");
        }
    }

    @Test
    public void theTest()
    {
        doubleTest();

        bigdecimalTest1();

        bigdecimalTest2();
    }
}
DwB
  • 37,124
  • 11
  • 56
  • 82
  • 1
    Your code does not prove that `BigDecimal` is bad, only that `double` has limited precision. Why is `double`'s limited precision the fault of `BigDecimal`? – tucuxi May 30 '19 at 23:40