英文:
How to cast to Number from String in java without knowing which number type is present in the String?
问题
I am writing a compiler, that reads from an input file, parses it and creates various kinds of tokens. Further, in parsing, upon getting a NumLitToken, I retrieve its number value stored as a String and want to save it as a Number for further stages of transformations.
I am not aware whether the String contains int/ float / long / double etc. so I am using NumberFormat.getInstance().parse(x) method and expecting an appropriate casted value, but I don't know why I am not getting a cast to Integer for int values.
Also, if there is any other way better to cast to Number from String, please enlighten me about it.
A small extract:
import java.text.*;
public class Main
{
    public static void main(String[] args) throws ParseException{
        String x = "100";
        Number o  = NumberFormat.getInstance().parse(x);
        System.out.println(o.getClass().toString());
        if(o instanceof Integer){
            System.out.println("int");
        }
    }
}
Output:
class java.lang.Long
UPDATE: Turns out, the method only returns long or double. What to do to get an appropriate cast to Number? Is there a better way rather than trying to cast for every number type?
英文:
<p>
I am writing a compiler, that reads from an input file, parses it and creates various kind of tokens. Further, in parsing, upon getting a NumLitToken , I retrieve its's number value stored as String and want to save it as a Number for further stages of transformations. <br> <br>
I am not aware whether the String contains int/ float / long / double etc. so i am using NumberFormat.getInstance().parse(x) method and expecting appropriate casted value, but i don't know why i am not getting a cast to Integer for int values.<br>
Also, if there is any other way better to cast to Number from String, please enlighten me about it.
</p>
A small extract:
import java.text.*;
public class Main
{
	public static void main(String[] args) throws ParseException{
		String x = "100";
		Number o  = NumberFormat.getInstance().parse(x);
		System.out.println(o.getClass().toString());
		if(o instanceof Integer){
		    System.out.println("int");
		}
	}
}
Output:
class java.lang.Long
<hr>
<p>
UPDATE : Turns out, the method only returns long or double. What to do to get appropriate cast to Number ? Is there a better way rather than trying to cast for every number type?
</p>
答案1
得分: 2
因为文档如此说明:
> 如果可能,返回一个 Long,否则返回一个 Double。
就是这么简单。
如果你只是想解析一个整数,int x = Integer.parseInt("500"); 就能完成任务!
英文:
Because the docs say so:
> Returns a Long is possible, otherwise a Double
Simple as that.
If you're just trying to parse an int, int x = Integer.parseInt("500"); does the job!
答案2
得分: 1
如在不同的回答中所提到的,这就是它的工作方式。
如果你想要一个根据其大小返回最合适类型的函数,apache-commons中的NumberUtils.createNumber可以为你完成。
英文:
As mentioned in a different answer it is how it works.
If you want to a function that returns the most appropriate type depending its size the apache-commons NumberUtils.createNumber will do that for you.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论