How to print 0.10 instead of 0.1 in Java?

2

The code below counts how many letters you have in a word and multiplies by 0.01. The problem is that when a 10-letter word, for example, is entered, it has an output of 0.10 instead of 0.1.

import java.util.Scanner;
import java.text.DecimalFormat;

public class MedidaDeTempo{

    public static void main(String[] args){
        Scanner ler = new Scanner(System.in);
        String teste;
        int C, tamanho, i = 0;
        double T;

        C = ler.nextInt();

        do{
            i++;
            teste = ler.nextLine();
            tamanho = teste.length();
            T = tamanho * 0.01;

            if(T > 0.00){
                DecimalFormat df = new DecimalFormat("0.##");
                String decimal = df.format(T);
                System.out.printf(decimal + "\n");
            }
        } while(i <= C);
    }
}
    
asked by anonymous 10.07.2018 / 22:31

1 answer

4

So?

System.out.format("%.2f%n", decimal);

You need to indicate the number of decimal places and not how it should be formatted. I hope it does not need accuracy, this solution does not guarantee this.

    
10.07.2018 / 22:35