One way to solve the problem is by using the class NumberFormat
.
NumberFormat
is the abstract base class for all number formats.
This class provides a standard way to format and parse numbers.
This class also provides methods to determine which localities have certain number formatting patterns and which names of those localities.
In this way as you want to format a decimal number, we should use the concrete class DecimalFormat
which is a subclass of NumberFormat
.
When reviewing class documentation DecimalFormat
we see that there is a special character pattern that we must "assemble" to generate the desired number formatting. These special characters must be passed in the constructor of the class DecimalFormat
.
Symbol |
Type |
Meaning |
0 |
number |
Digit |
# |
number |
Digit, omit the zero |
. |
number |
Decimal separator |
*I am only representing the values I used in the answer, for more options see the class javadoc DecimalFormat
.
Follow an example:
double[] exemplo = {24.00, 8.00, 16.00, 14.00, 10.00};
NumberFormat nf = new DecimalFormat("00.00");
for (int l = 0; l < 5; l++){
System.out.println(nf.format(exemplo[l]));
}
So must have the expected result.
If you want it is also possible to "format" the decimal separator (swap semicolon for example), for this you will need to use the class DecimalFormatSymbols
This class represents the set of symbols (such as the decimal separator, the grouping separator and so on) required in the class DecimalFormat
to format numbers.
An interesting use case would be to customize a formatting.
Follow an example:
DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.getDefault());
dfs.setDecimalSeparator(',');
double[] exemplo = {24.00, 8.00, 16.00, 14.00, 10.00};
NumberFormat nf = new DecimalFormat("00.00", dfs);
for (int l = 0; l < 5; l++){
System.out.println(nf.format(exemplo[l]));
}
In the above example it would be simpler to use a locale that already has a comma as a decimal separator.
DecimalFormatSymbols dfs = new DecimalFormatSymbols(new Locale("pt", "BR"));
This way you do not depend on the default locale (which can even be changed by any application running in the same JVM (since this is a shared resource), that is, it is something you do not control). Besides, if the default locale is some other, it may have other different settings, like the thousands separator (it’s okay that in this case it’s not being used, but it’s better to already use the correct locale than to use the default and quit changing everything)
A list of locales that the Java language has support can be seen here.
It is also possible to format monetary values using the currency, follow an example:
NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
nf.setMinimumIntegerDigits(2);
System.out.println(nf.format(24.00));
//R$ 24,00
This way it will already format as currency ( which already includes the "R$"). Changing the locales will also change the presentation.
This answers your question? Fill string with zeros on the left
– Renato Junior