-2

so I got a Problem coding in Java.

I have some doubles declared and I need to calculate with them. But I dont know who I convert the doubles to a result that can be posted in a JLabel as a String. It always says something like: cannot convert double to String. I have tried toString method but I am either doing it wrong or it just doesnt work. Any ideas for a working code?

  • 3
    Help us help you - share the code you have so far, the output you're getting and the output you're actually getting – Mureinik Sep 13 '21 at 12:14
  • Generally speaking, you're going to want to print the value to, say, 2 d.p. so look at `String.format("%.2f", yourDouble)` – g00se Sep 13 '21 at 12:15
  • @Mureinik Hey I was not able to share the code tho. I have solved it now anyways but simply using the valueOf(). But thank you anyways :) – robin kemper Sep 13 '21 at 13:00

1 Answers1

0

You can use String#valueOf.

Demo:

public class Main {
    public static void main(String[] args) {
        double val = 12.34;
        String strVal = String.valueOf(val);
        System.out.println(strVal);
    }
}

Output:

12.34
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110