我可以知道如何将字符串转义为十进制格式吗?
// currencySymbolPrefix can be any string.
NumberFormat numberFormat = new DecimalFormat(currencySymbolPrefix + "#,##0.00");
如何转义 currencySymbolPrefix
,以便它可以包含包括“#”在内的任何字符,并且不会被解释为模式之一。
请勿推荐
NumberFormat numberFormat = new DecimalFormat("#,##0.00");
final String output = currencySymbolPrefix + numberFormat.format(number);
因为我供应商的方法只接受单一的 NumberFormat。
最佳答案
您可以使用撇号来引用:
String pattern = "'" + currencySymbolPrefix + "'#,##0.00";
NumberFormat numberFormat = new DecimalFormat(pattern);
如果 currencySymbolPrefix
本身可能包含撇号,您需要通过将它们加倍来转义它们:
String pattern = "'" + currencySymbolPrefix.replace("'", "''") + "'#,##0.00";
关于java - 如何转义要传递给 DecimalFormat 的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3540790/