我是 Android 开发人员。我从网络服务结果 45.0000 中获得。我存储了 ArrayList。但我只想在两位数之后显示它。
仅示例输出 45.00。请给我解决方案。
使用DecimalFormat
类来实现这一点。
DecimalFormat df=new DecimalFormat("##.##");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
你可以使用String.format
,它使用指定的格式字符串和参数返回一个格式化的字符串
String.format("$%.2f", value);
还String.format
使用您的Locale
整数索引 = 0;for (; index < yourList.size (); index++) { String tmpvalue = yourList.get(index); String conValue = String.format("$%.2f", Float.parse(tmpvalue)); yourList.set(index, conValue); 索引++;}