-1

I'm building a calculator app which take up math expressions, parses it and displays the results, for that I use Javaluator.

Something like:

String expression = "(2^3-1)";
Double result = new DoubleEvaluator().evaluate(expression);

I use two textView for this: one for displaying user input (expression) and the other for displaying the results which are a Double.

Everything works fine but I would like to get rid of the float that is returned after each operation: e.g: 10 * 10 = 100.0. I tried something like finResult = result.intValue(); works but is broken for division operations. e.g: 2 / 3 = 0.

Is there a way to fix that?

Krul
  • 139
  • 8
  • How would you handle `2 / 3` then? – ifly6 Jun 08 '18 at 12:24
  • Would be 0.6. But only zero is retained here – Krul Jun 08 '18 at 12:25
  • int value = (int) 6.14; returns 6 – ankur uniyal Jun 08 '18 at 12:26
  • 1
    When you do `intValue` or casting, it doesn't round. It truncates: i.e. 0.18927 = 0 and 0.99 = 0. – ifly6 Jun 08 '18 at 12:27
  • 1
    @Krul I'll rephrase the question: *I would like to get rid of the float that is returned* - how would you handle `2 / 3` without a `float` then? You can't store `0.6666` to an `int` – BackSlash Jun 08 '18 at 12:27
  • Casting doesn't seem to work. or am I doing things wrong... – Krul Jun 08 '18 at 12:28
  • 1
    Apparently he wants to truncate it to the tenth's place? He answered my question asking that with `2 / 3 === 0.6`. If it rounded correctly, it would be 0.7, since 0.66 (repeating) always ends with a 7. – ifly6 Jun 08 '18 at 12:28
  • 1
    @ifly6 Still, this cannot fit into an `int`. OP said he doesn't want decimal places, but it seems he wants them. A bit unclear to me. – BackSlash Jun 08 '18 at 12:30
  • 2
    Honestly, he probably wants [`DecimalFormat`](https://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html) – ifly6 Jun 08 '18 at 12:32
  • Maybe, something like this: https://stackoverflow.com/questions/703396/how-to-nicely-format-floating-numbers-to-string-without-unnecessary-decimal-0 – Daria Pydorenko Jun 08 '18 at 12:35
  • @BackSlash but for int I don't need a float neither. – Krul Jun 08 '18 at 12:48
  • I honestly have no idea what you're asking here. Could you add some examples of inputs and expected outputs? Right now judging from the title, it seems that you really just need the integer value without decimal places. And for `2/3` the expected result is `0`. – QBrute Jun 08 '18 at 12:52
  • I just don't need the result be displayed with extra 0 for rounded number, exemple: 100.0 should bve 100. – Krul Jun 08 '18 at 13:00
  • @Krul but then you say that `2 / 3` should give you `0.6`. Why do you think you don't need a float here? – BackSlash Jun 08 '18 at 13:02
  • @Krul So you want to remove **trailing zeroes**? – QBrute Jun 08 '18 at 13:03
  • @BackSlash Sorry If I wasn't clear enoughn e,glish is not my primary language. float is needed for division but what I was asking is how to get rid of the trailing zero – Krul Jun 08 '18 at 13:05
  • @QBrute indeed. – Krul Jun 08 '18 at 13:07
  • 1
    Possible duplicate of [How to nicely format floating numbers to String without unnecessary decimal 0?](https://stackoverflow.com/questions/703396/how-to-nicely-format-floating-numbers-to-string-without-unnecessary-decimal-0) – ifly6 Jun 08 '18 at 16:23
  • Solved @ifly6. DecimalFormat was what I needed – Krul Jun 09 '18 at 13:22

1 Answers1

0

For getting rid of trailing zero, you can use DecimalFormat and its method setMinimumFractionDigits(0).

DecimalFormat dc = new DecimalFormat();
dc.setMinimumFractionDigits(0);

String finResult = dc.format(result);
Daria Pydorenko
  • 1,754
  • 2
  • 18
  • 45
  • 1
    This one worked, I just added the pattern. Something like this: `DecimalFormat df = new DecimalFormat("####.#######");` – Krul Jun 09 '18 at 13:20