-2
public String solve() {
        ss = log(pow(y, -sqrt(abs(x)))) * (x + y / 2) + sin(atan(z));
        result = toString(ss);
        return result;
    }

Don't get how do I convert ss to string. I neen return result in String format

Shigure
  • 1
  • 3

2 Answers2

0

The easiest way is to simply concatenate it with an empty String which converts it automatically:

String stringFromDouble = ss + "";

Alternatives:

 String stringFromDouble = Double.toString(ss);
 String stringFromDouble = String.valueOf(ss)
 String stringFromDouble = new Double(ss).toString()
Markus Steppberger
  • 618
  • 1
  • 8
  • 18
0

To answer your question directly, you can do this:

result = Double.toString(ss);

However, there are at least two problems here:

  1. Your solve() function is attempting to do two unrelated tasks: a) find the solution of some mathematical equation and b) format the solution into a human readable format. By the functions name, I think it should only do the first thing and another function should do the second.

  2. Using Double.toString() leaves a lot of room for variation. This is primarily due to the nature of how a computer represents a decimal number as a double. This is beyond the scope of this answer, but I suggest you read Is floating point math broken? for more details. To provide more control over the exact format, you should use NumberFormat instead.

Aside:

I see that you do not declare result or ss in the code you posted here. If you are declaring these as class fields such as private double ss;, I suggest that you declare them as local variables instead. In general, you should declare variables in the narrowest scope possible.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268