简体   繁体   中英

Inserting custom String between number and decimal - Java DecimalFormat

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.

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.

You might consider using String.format(String pattern, Object... arguments). You can pass your simply formatted numbers as arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM