使用DecimalFormat进行格式化会引发异常-“无法将给定对象格式化为数字". [英] Formatting using DecimalFormat throws exception - "Cannot format given Object as a Number"

查看:683
本文介绍了使用DecimalFormat进行格式化会引发异常-“无法将给定对象格式化为数字".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来像是一个重复的问题,但是我尝试了以下所有链接,但找不到正确的答案.

This might look like a repeated question but I tried in all the below links and can't get a proper answer.

无法将给定对象格式化为数字组合框

非法参数异常

但是我没有发现问题所在.这是我的代码

But I'm not getting what's wrong. Here is my code

DecimalFormat twoDForm = new DecimalFormat("#.##");
double externalmark = 1.86;
double internalmark = 4.0;
System.out.println(String.valueOf((externalmark*3+internalmark*1)/4));
String val = String.valueOf((externalmark*3+internalmark*1)/4);
String wgpa1=twoDForm.format(val); // gives exception
String wgpa2=twoDForm.format((externalmark*3+internalmark*1)/4)); // works fine
System.out.println(wgpa1);

format方法采用对象类型参数,所以这就是为什么我传递了一个给出异常的String对象

The format method takes Object type argument, so that's why I passed a String object which gives exception

线程"main"中的异常java.lang.IllegalArgumentException:无法 将给定对象的格式设置为数字.

但是当我给double值作为参数时,程序运行良好.但是,如果该方法是用Object类型参数定义的,为什么我在传递String时却得到了异常而在传递double时却没有得到了异常?

But when I give double value as argument the program works fine. But if the method is defined with Object type argument why I'm getting an exception while passing a String and not getting exception while passing double?

推荐答案

DecimalFormatformat()方法已重载.

在实际情况下,您正在调用:

In the working case, you are invoking :

 public final String format(double number)

在失败的情况下,您正在调用:

And in the failing case, you are invoking :

 public final String format (Object obj) 

第一个方法采用一个非常具体的参数.它需要一个double.

The first method takes a very specific argument. It expects a double.

第二种情况不是这样,第二种情况接受的类型非常广泛:Object,因此在运行时对传递的类型进行检查.

This is not the case of the second one, which the type accepted is very broad : Object and where so the check on the type passed is performed at runtime.

通过提供一个不是double而是String的参数,调用的方法是第二个方法.

By providing a argument that is not a double but a String, the method invoked is the second one.

在内部,此方法依赖于format(Object number, StringBuffer toAppendTo, FieldPosition pos)方法,该方法期望使用number自变量,该自变量是Number类(ShortLong,... Double)的实例. :

Under the hood, this method relies on the format(Object number, StringBuffer toAppendTo, FieldPosition pos) method that expects to a number argument that is an instance of the Number class (Short, Long, ... Double):

@Override
public final StringBuffer format(Object number,
                                 StringBuffer toAppendTo,
                                 FieldPosition pos) {
    if (number instanceof Long || 
        number instanceof Integer ||                   
        number instanceof Short || 
        number instanceof Byte ||                   
        number instanceof AtomicInteger ||
        number instanceof AtomicLong ||
        (number instanceof BigInteger && ((BigInteger)number).bitLength () < 64)) {

        return format(((Number)number).longValue(), toAppendTo, pos);
    } else if (number instanceof BigDecimal) {
        return format((BigDecimal)number, toAppendTo, pos);
    } else if (number instanceof BigInteger) {
        return format((BigInteger)number, toAppendTo, pos);
    } else if (number instanceof Number) {
        return format(((Number)number).doubleValue(), toAppendTo, pos);
    } else {
        throw new IllegalArgumentException("Cannot format given Object as a Number");
    }
}

但是,当您将其传递给String实例时,情况并非如此.

But it is not the case as you passed to it a String instance.

要解决此问题,可以像成功案例中那样传递double原语,或者将String转换为Number的实例,例如DoubleDouble.valueOf(yourString).
我建议采用第一种方法(传递double),因为它在已经使用double原语的代码中更加自然.
第二个需要从StringDouble的附加转换操作.

To solve the problem, either pass a double primitive as in the success case or convert your String into an instance of Number such as Double with Double.valueOf(yourString).
I advise the first way (passing a double) as it is more natural in your code that already uses double primitives.
The second one requires a additional conversion operation from String to Double.

这篇关于使用DecimalFormat进行格式化会引发异常-“无法将给定对象格式化为数字".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆