printf как делать плавающие точки с ведущими нулями

Я знаю, как сделать X количество ведущих нулей, и я знаю, как сделать X количество десятичных точек. Но как я их обоим?

Я ищу, чтобы иметь 4 ведущих нуля до десятичной точности 2: 0000.00. Следовательно, 43,4 будет 0043,40

Попробуйте эту строку printf ( C , Perl , PHP ):

 "%07.2f" 

Вот код, который вам нужен:

 float myNumber = 43.4; DecimalFormat formatter = new DecimalFormat("0000.00"); //use # for optional digits instead of 0 System.out.println(formatter.format(myNumber)); 

Java: используйте класс Formatter . Примеры ожидаемого использования:

 StringBuilder sb = new StringBuilder(); // Send all output to the Appendable object sb Formatter formatter = new Formatter(sb, Locale.US); // Explicit argument indices may be used to re-order output. formatter.format("%4$2s %3$2s %2$2s %1$2s", "a", "b", "c", "d") // -> " dcba" // Optional locale as the first argument can be used to get // locale-specific formatting of numbers. The precision and width can be // given to round and align the value. formatter.format(Locale.FRANCE, "e = %+10.4f", Math.E); // -> "e = +2,7183" // The '(' numeric flag may be used to format negative numbers with // parentheses rather than a minus sign. Group separators are // automatically inserted. formatter.format("Amount gained or lost since last statement: $ %(,.2f", balanceDelta); // -> "Amount gained or lost since last statement: $ (6,217.58)"