Java 如何将数字(如 int)转换为“数字”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23320267/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:51:53  来源:igfitidea点击:

How to cast numbers (like int) to "Number"?

javacastingnumbers

提问by Salman

This could be basic question but I could not find something useful.

这可能是基本问题,但我找不到有用的东西。

Question is: How to convert doubleor intvalue to Numbertype ( to be more specific oracle.jbo.domain.Number)

问题是:如何转换doubleint值到Number类型(更具体oracle.jbo.domain.Number

I tried the following:

我尝试了以下方法:

for integer values

对于整数值

         int i=9;
         Integer y=new Integer(i);
         oracle.jbo.domain.Number num=(oracle.jbo.domain.Number)y;

for float values

对于浮点值

         double i=9.5;
         Double y=new Double(i);
         oracle.jbo.domain.Number num=(oracle.jbo.domain.Number)y;

But I always get incompatible types error in both cases:

但在这两种情况下,我总是遇到不兼容的类型错误:

Type 'oracle.jbo.domain.Number' incompatible with 'java.lang.Integer'

and

Type 'oracle.jbo.domain.Number' incompatible with 'java.lang.Double'

if the conversion did not work, then how to simply create Numberobject in java

如果转换不起作用,那么如何Number在java中简单地创建对象

采纳答案by Braj

You do not need to cast it. just use constructor of Numberclass.

你不需要投射它。只需使用Number类的构造函数。

int value = 5;
oracle.jbo.domain.Number num = new oracle.jbo.domain.Number(value);

回答by aga

Take a look at documentation. You can construct new Numberobjects like so:

查看文档。您可以Number像这样构造新对象:

Number dNumber = new Number(9.5);
Number iNumber = new Number(9);