Java – how to escape a string to be passed to decimalformat
•
Java
Can I know how to escape a string to decimal format?
// currencySymbolPrefix can be any string. NumberFormat numberFormat = new DecimalFormat(currencySymbolPrefix + "#,##0.00");
How to avoid currencysymbolprefix so that it may contain any character containing '#' and will not be interpreted as one of the modes
Please don't suggest
NumberFormat numberFormat = new DecimalFormat("#,##0.00"); final String output = currencySymbolPrefix + numberFormat.format(number);
Because my supplier's method only accepts a single numberformat
Solution
You can use apostrophes to refer to:
String pattern = "'" + currencySymbolPrefix + "'#,##0.00"; NumberFormat numberFormat = new DecimalFormat(pattern);
If the currentysymbolprefix itself may contain apostrophes, you need to escape by doubling them:
String pattern = "'" + currencySymbolPrefix.replace("'","''") + "'#,##0.00";
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码