将 java.lang.String 转换为 java.lang.Number

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18081126/
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-11 22:16:02  来源:igfitidea点击:

Convert java.lang.String to java.lang.Number

java

提问by Shashi

I have a String object and I need to convert to java.lang.Number.

我有一个 String 对象,我需要转换为 java.lang.Number。

Number num = null;
Object cellContents = ".475";

If I try to cast the cellContentsto Number directly,

如果我尝试将cellContents直接转换为 Number,

num = (Number) cellContents;

it throws an exception:

它抛出一个异常:

E[java.lang.ClassCastException: java.lang.String]:  : java.lang.ClassCastException: java.lang.String

I searched but could not get a complete answer as to how I can achieve this. Please help!!!

我进行了搜索,但无法获得有关如何实现这一目标的完整答案。请帮忙!!!

采纳答案by Andy Thomas

You can't just cast the reference type. You're getting an exception because the String object to which it points is not a Number object.

你不能只转换引用类型。您收到一个异常,因为它指向的 String 对象不是 Number对象

You can, however, cast the reference to a String, if you know it's a String. You can convert it to a real value with Double.valueOf( String )or Float.valueOf( String ). Once you get a double, you can use auto-boxingto turn it into a Double, which isa Number.

但是,如果您知道它是一个字符串,您可以将引用转换为一个字符串。您可以使用Double.valueOf( String )或将其转换为实际值Float.valueOf( String )。一旦你得到一个双,你可以使用自动装箱把它变成一个双,也就是一个数字。

Object cellContents = ".475";
Number num = null;
if ( cellContents instanceof String ) {
   try {
       double d = Double.valueOf( (String) cellContents );
       num = (Double) d; // Auto-boxing
   }
   catch ( NumberFormatException e ) {
     ...
  }
}
else {
    ...
}

回答by Thilo

Try

尝试

num = new BigDecimal((String)cellContents);

回答by Rohit Jain

You can't cast a String to a Number, because they are not covariant (they don't fall in the same inheritance hierarchy).

您不能将 String 转换为 a Number,因为它们不是协变的(它们不属于同一继承层次结构)。

You can convert the Stringto floator doubleusing Float#parseFloat()and Double#parseDouble()respectively, which is what you would need in most cases.

您可以分别转换Stringfloatdouble使用Float#parseFloat()Double#parseDouble(),这在大多数情况下是您需要的。

Using Float#valueOf(String)and Double#valueOf(String)creates instances of wrapper classes.

使用Float#valueOf(String)Double#valueOf(String)创建包装类的实例。

So, depending upon what you need, you can use any of them. I'll show here the first parseXXXmethods:

因此,根据您的需要,您可以使用它们中的任何一个。我将在这里展示第一种parseXXX方法:

float strToFloat = Float.parseFloat(".475");
double strToDouble = Double.parseDouble(".475");



但是,如果您正在处理货币价值或其他一些需要关注浮点精度的价值,那么您应该BigDecimalBigDecimal改用:

BigDecimal number = new BigDecimal(".475");

回答by Sachin Verma

You can not simply convert a String into Number directly as they are not in same hierarchy.But good news is Java Wrapper API do most of the task for you automatically IE, convert a valid String into Wrapper you want and throws a NumberFormatException if the passed String is not a valid Number or other Wrapper.

您不能简单地将 String 直接转换为 Number,因为它们不在同一层次结构中。但好消息是 Java Wrapper API 自动为您完成大部分任务,将有效的 String 转换为您想要的 Wrapper,如果通过,则抛出 NumberFormatException字符串不是有效的数字或其他包装器。

You should use.

你应该使用。

Integer.parseInt(String) throws NumberFormatException

OR

或者

Double.parseDouble(String) throws NumberFormatException

they both return Number Integer and Double respectively and both ARE-A Number.

它们都分别返回 Number Integer 和 Double 并且都是 ARE-A Number。

回答by Abhishek Singh

You cannot cast ".475" to Integer. However you can cast it to Float or Double using

您不能将“.475”转换为整数。但是,您可以使用将其转换为 Float 或 Double

float x = Float.parseFloat(".475");

or

或者

double y  = Double.parseDouble(".475");

You should search more about Wrapper Classes in Java and AutoBoxing in java to get basic clear. Happy Coding :)

您应该搜索更多关于 Java 中的 Wrapper 类和 Java 中的 AutoBoxing 以获得基本的清晰。快乐编码:)

回答by elfekz

Firstly, you should cast your object to string.

首先,您应该将对象转换为字符串。

You can try something like the following code:

您可以尝试类似以下代码:

Number num = null; 
Object cellContents = ".475";
num = cellContents.toString();  
num = (Number) num;