我使用DecimalFormat类将数字格式化为字符串,反之亦然。
在我的数据库中,Java双精度以特定方式表示:例如,128.88存储为“00012888”。为此,我使用“00000.00”模式并删除“。”格式化后的小数分隔符,这不是一个智能解决方案。
有没有办法告诉DecimalFormat不要使用任何小数分隔符?我可以让它理解整数部分的位置和表示十进制数的字符串中的小数部分吗?
我宁愿在转换之前不必乘法,除法,截断或舍入。 这有什么神奇的模式吗?或者DecimalFormat类不灵活?
我确切地说我不能使用String.format()方法,因为我在Java 1.4平台上工作。
感谢您的帮助。
答案 0 :(得分:0)
DecimalFormat
并不足以做到这一点。
你可以使用这样的方法:
double getD(String fixedWidthNumber) {
return Double.valueOf(fixedWidthNumber.substring(0,6)+"."+fixedWidthNumber.substring(6,8));
}
答案 1 :(得分:0)
BigDecimal value = new BigDecimal("234.33");
DecimalFormat decFrm = new DecimalFormat("000.00");
char decSep = decFrm.getDecimalFormatSymbols().getDecimalSeparator();
String resultStr = decFrm.format(value).replace(Character.toString(decSep), "");
结果:3423433