Hey Guys,

I'm want to output a calculation to a JLabel. Example:

double num1 = 14;
double num2 = 3;
double ans;

ans = num1/num2;

JLabel jl = new JLabel();
add(jl);

jl.setText("The Answer is: " + ans);

Currently the answer for this will be 4.666666667. Now if this was a System.out statement I could have just done:
System.out.printf("The answer is: %.2f", ans);. However, I can't. So what can I do to make the number diplayed in the JLabel look like 4.66.

Thank-you

Recommended Answers

All 3 Replies

You want java.lang.String.format as in:

jl.setText(String.format("The answer is: %.2f", ans));

use NumberFormat or DecimalFormat for number instance, set proper RoundingMode and decimal places if needed

for example

import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Formatter;

public class round {

    public static double roundTo2Places(double value) {
        assert value >= Long.MIN_VALUE / 100 && value <= Long.MAX_VALUE / 100;
        long digits = (long) (value < 0 ? value * 100 - 0.5 : value * 100 + 0.5);
        return (double) digits / 100;
    }

    public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getNumberInstance();
        nf.setMaximumFractionDigits(2);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        nf.setRoundingMode(RoundingMode.HALF_DOWN);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        nf.setRoundingMode(RoundingMode.FLOOR);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        nf.setRoundingMode(RoundingMode.CEILING);
        System.out.println("Default rounding mode: " + nf.getRoundingMode());
        System.out.println("123.454 rounds to " + nf.format(123.454));
        System.out.println("123.455 rounds to " + nf.format(123.455));
        System.out.println("123.456 rounds to " + nf.format(123.456));
        System.out.println();
        Formatter fmt = new Formatter();
        fmt.format("%.2f", 123.1234567);
        System.out.println(fmt);
        int number = 1500;
        String formatted = String.format("%07d", number);
        System.out.println("Number with leading zeros: " + formatted);
        fmt = new Formatter();
        fmt.format("%1.4f", 1234567890.123456789);
        System.out.println(fmt);
    }
}

Thanks fellas! bguild had the answer I was looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.