0

I am building a simple inventory and sales app, so i wrote a profit calculation method in android to get the profits from all products as total profit and display it in TextView on the app. The values are in double so I wrote this method below to get the total profit but the output is a bit weird, it show's something like "2.45116E7" or "1.22558E7" and so on as it changes each time I run the app but has same format as the total profit. So I added Log.d to trace the outputs inside of the method and I realized that the method does get the profit values from each product and adds them up as the total profit but as soon as the total profit reaches 10,000,000 or above it changes to that output. Please I need help to understand what is wrong and possible solutions. See the method below with the screenshot of the Log.d outputs.

private double getStoreProfits() {
    for (Product product : products) {
        Log.d("Profit", String.valueOf(product.getProfit()));
        totalProfit += product.getProfit();
        Log.d("Total Profits", String.valueOf(totalProfit));
    }
    return totalProfit;
}

The output:

Screenshot of the Log.d outputs

osebidev
  • 19
  • 8
  • 1
    I'd suggest using `BigDecimal` instead of `double` to start with, for currency operations. (Or compute everything in integers using BigInteger.) – Jon Skeet Jul 27 '20 at 14:09
  • do you understand that `E` is a specific notation in math ? is that part of your question ? – a_local_nobody Jul 27 '20 at 14:11
  • @JonSkeet thanks, I tried to use BigDecimal but Firebase (where I stored the data) throws an exception that it does not support BigDecimals, as its a business app, i wanted to use doubles or BidDecimals but since Firebase doesn't support that any other options? – osebidev Jul 27 '20 at 14:14
  • If you use [System.out.printf](https://www.baeldung.com/java-printstream-printf#2-float-and-double-formatting), you can specify the width. – Botahamec Jul 27 '20 at 14:15
  • 1
    @a_local_nobody I do, but I just couldn't interpret it in this context, can you help and my question is how do i make the actual digits of my calculation show up instead of that expression? – osebidev Jul 27 '20 at 14:16
  • @Botahamec but i want to set the value to a textview in my android app, System.out.printf won't do that on android app, or will it? – osebidev Jul 27 '20 at 14:18
  • System.out.format("%.0f",profit); Where profit is your value. This will print the full value with no zeros after the decimal point. For more information check https://docs.oracle.com/javase/tutorial/java/data/numberformat.html – ArbitraryChoices Jul 27 '20 at 14:18
  • @osebidev You should use [String.format](https://www.javatpoint.com/java-string-format) then – Botahamec Jul 27 '20 at 14:20
  • @Appu okay I will check that out now – osebidev Jul 27 '20 at 14:24
  • @Botahamec Thanks, i will try it out – osebidev Jul 27 '20 at 14:24
  • If Firebase storage is one of the requirements, please edit your question to indicate that. There's no mention of it at the moment. (But you can use `BigDecimal` and store the result as a string.) – Jon Skeet Jul 27 '20 at 14:31

1 Answers1

1

You can replace the String.valueOf(var) with String.format("%.2f", var).

This will give you all of the digits before the decimal point, followed by two digits after the decimal.


Of course, as Jon said, it would be a bit better in this case to use a BigDecimal. You can either convert it into a float or a double and use the method I already described (using floatValue() or doubleValue()) or you can use the toPlainString() method.

Botahamec
  • 263
  • 3
  • 8
  • Thanks a million! That fixed it! Please can you point me to resources to under this and other related concepts better? – osebidev Jul 27 '20 at 14:40
  • For anyone with this issue, this question has more details on it https://stackoverflow.com/questions/16098046/how-do-i-print-a-double-value-without-scientific-notation-using-java – osebidev Jul 27 '20 at 14:55