为什么我得到构造函数BigDecimal(Number)是未定义的?

时间:2016-04-14 14:44:24

标签: java numbers jasper-reports classcastexception bigdecimal

我在Jasper Reports中创建了一个变量类名为java.math.BigDecimal的变量。我将表达式设置为:

new BigDecimal(PRODUCT($F{CurrentValue},100)).divide($V{current_val_total})

但我一直收到错误:

The constructor BigDecimal(Number) is undefined 

net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. The constructor BigDecimal(Number) is undefined
            value = new BigDecimal(PRODUCT (((java.math.BigDecimal)field_CurrentValue.getValue()),100)).divide(((java.math.BigDecimal)variable_current_val_total.getValue())); //$JR_EXPR_ID=18$
                    <--------------------------------------------------------------------------------->
2. The constructor BigDecimal(Number) is undefined
            value = new BigDecimal(PRODUCT (((java.math.BigDecimal)field_CurrentValue.getOldValue()),100)).divide(((java.math.BigDecimal)variable_current_val_total.getOldValue())); //$JR_EXPR_ID=18$
                    <------------------------------------------------------------------------------------>
3. The constructor BigDecimal(Number) is undefined
            value = new BigDecimal(PRODUCT (((java.math.BigDecimal)field_CurrentValue.getValue()),100)).divide(((java.math.BigDecimal)variable_current_val_total.getEstimatedValue())); //$JR_EXPR_ID=18$
                    <--------------------------------------------------------------------------------->
3 errors
.
at   net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:215)
at net.sf.jasperreports.eclipse.builder.JasperReportCompiler.compileReport(JasperReportCompiler.java:195)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.compileJasperDesign(ReportControler.java:439)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler.access$15(ReportControler.java:416)
at com.jaspersoft.studio.editor.preview.view.control.ReportControler$5.run(ReportControler.java:341)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

PRODUCT返回java.lang.Number,请参阅Jasper Reports Functions

java.math.BigDecimal中的无构造函数需要java.lang.Number

你需要使用api中可用的一个构造函数,我建议BigDecimal(double val),因此调用doubleValue()上的java.lang.Number函数

new BigDecimal(PRODUCT($F{CurrentValue},100).doubleValue()).divide($V{current_val_total})

或使用String

Number
new BigDecimal(PRODUCT($F{CurrentValue},100).toString()).divide($V{current_val_total})

哪一个“最佳”使用取决于实际的Number类,有关详细信息,请参阅Convert Java Number to BigDecimal : best way。然而,在具有正常舍入的报告生成中,它将没有太大的区别。