Java euro符号转换为货币simbol
我正在尝试使用NumberFormat将双变量格式化为价格字符串(例如:59,00欧元) 以下是我写的方法:Java euro符号转换为货币simbol,java,spring,pdf,itext,number-formatting,Java,Spring,Pdf,Itext,Number Formatting,我正在尝试使用NumberFormat将双变量格式化为价格字符串(例如:59,00欧元) 以下是我写的方法: private String formatValue (double price){ NumberFormat f = NumberFormat.getCurrencyInstance(Locale.ITALIAN); String value = f.format(new BigDecimal(price)); return value; } 然后,我使用iText库将返
private String formatValue (double price){
NumberFormat f = NumberFormat.getCurrencyInstance(Locale.ITALIAN);
String value = f.format(new BigDecimal(price));
return value;
}
然后,我使用iText库将返回值写入pdf字段
form.setField("value", formatValue(price));
现在,当我在浏览器的pdf查看器(如chrome或firefox)中打开生成的pdf时,该字段如下所示:
59,00欧元
但当我在AdobeReader中打开它,或者我尝试实际打印时,它看起来像
·59,00
如果我在debug中打开变量值,我会看到格式为·59,00的字符串
我做错了什么
我解决了使用DecimalFormat代替NumberFormat,并在utf-8编码中传递欧元符号的问题(mwhs的输入[谢谢])
您可能正在使用单独的编码。浏览器可能使用UTF-8,而adobe reader可能使用ANSI或其他UTF本地化。(注意,这些不一定是他们使用的编码,只是一个示例)因此请检查您的首选项,然后重试。您可以使用实际的UTF-8欧元符号,这使代码更具可读性
private static final DecimalFormat EURO_FORMAT = new DecimalFormat("€0.00");
private String formatValue (double price){
return EURO_FORMAT.format(price);
}
但是java.util.NumberFormat是一个更好的选择,因为您可以使用区域设置来获得正确的数字格式。在法国、德国和荷兰,使用逗号代替小数点前的小数点。但数千人的格式是不同的。请参见下面的示例:
public class NumberFormatLocaleExample {
public static void main(final String[] args) {
double price = 1249.69;
System.out.println(formatValueGermany(price));
System.out.println(formatValueFrance(price));
}
private static final NumberFormat EURO_FORMAT_GER = NumberFormat.getCurrencyInstance(Locale.GERMAN);
private static String formatValueGermany(double price){
return String.format("€%s", EURO_FORMAT_GER.format(price));
}
private static final NumberFormat EURO_FORMAT_FRANCE = NumberFormat.getCurrencyInstance(Locale.FRENCH);
private static String formatValueFrance(double price){
return String.format("€%s", EURO_FORMAT_FRANCE.format(price));
}
}
你读过这篇关于Unicode和字符集的文章吗?这将帮助你更好地理解你的问题!谢谢你的意见,我解决了
public class NumberFormatLocaleExample {
public static void main(final String[] args) {
double price = 1249.69;
System.out.println(formatValueGermany(price));
System.out.println(formatValueFrance(price));
}
private static final NumberFormat EURO_FORMAT_GER = NumberFormat.getCurrencyInstance(Locale.GERMAN);
private static String formatValueGermany(double price){
return String.format("€%s", EURO_FORMAT_GER.format(price));
}
private static final NumberFormat EURO_FORMAT_FRANCE = NumberFormat.getCurrencyInstance(Locale.FRENCH);
private static String formatValueFrance(double price){
return String.format("€%s", EURO_FORMAT_FRANCE.format(price));
}
}