Inserting custom String between number and decimal - Java DecimalFormat
-
15-02-2021 - |
Pregunta
Hello and thank you in advance for the help.
I am having some trouble formatting using a Java function to mark up a price in HTML.
It seems that, no matter what I do, I cannot insert custom content between the numbers and the decimal (throws Illegal Argument Exception). Is there any known way to achieve the following:
NumberFormat nf = getNumberFormat("'<span class=\"dollars\">'##'</span></span class=\"decimal\">'.'</span></span class=\"cents\">'00'</span>'", locale);
nf.format(number);
Assume locale and number are correctly initialized.
Solución
If you look at the docs for DecimalFormat
you'll see that they talk about the prefix and the suffix text - but not putting arbitrary text within a number.
It sounds like you should basically write this bit of formatting yourself - possibly using DecimalFormat
for each section of the number.
Otros consejos
You might consider using String.format(String pattern, Object... arguments). You can pass your simply formatted numbers as arguments.