java双精度长整形,Java将双精度转换为长异常

1- long xValue = someValue;

2- long yValue = someValue;

3- long otherTolalValue = (long)(xValue - yValue);

That line of code give me the following exception:

java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.Long.

Update :

code piece:

StackedBarChart sbc = new StackedBarChart();

XYChart.Series series = new XYChart.Series();

series.getData.add(new XYChart.Data("X1",150));

series.getData.add(new XYChart.Data("X2",50));

sbc.getData.add(series);

long dif = getDif(sbc);

long getDif(XYChart barChart){

XYChart.Series series = (XYChart.Series).getData().get(0);

// X1 at zero position i dont have to use iIterator now.

XYChart.Data seriesX1Data = series.getData().get(0);

XYChart.Data seriesX2Data = series.getData().get(1);

long x1Value = seriesX1Data.getYValue();

long x2Value = seriesX1Data.getYValue();

// line - 3 - exception on the next line

// -4- long value = (x1Value) - (x2Value);

long value = (long)(x1Value) - (long)(x2Value);

return value;

}

After debug i found that.

seriesX1Data,seriesX2Data contains double values as the passed chart has Number type but getYvalue() return long that is why program crash at runtime with that exception but when i cast in line why cast not succeed. i think that compiler see that the type already long !.

解决方案

It's impossible

long xValue = someValue;

long yValue = someValue;

long otherTolalValue = (long)(xValue - yValue);

neither of the 3 lines can produce java.lang.ClassCastException

Assuming someValues is Double,

Double someValue = 0.0;

it would give compile error: Type mismatch: cannot convert Double to long

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值