将字符串解析为数字 [英] Parse String into Number

查看:137
本文介绍了将字符串解析为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,可以存储数字对象。该列表将通过解析字符串列表来填充,其中每个字符串可以表示任何Number的子类。



如何将字符串解析为通用数字,而不是数据不能被实例化,因为它是一个抽象类。我建议在Numbers中传递,但是如果你设置在字符串上,你可以使用任何子类来解析它们,

  Number num = Integer.parseInt(myString); 

  Number num = NumberFormat.getInstance()。parse(myNumber); 

@See NumberFormat


I have a list which will store Number objects. The list will be populated by parsing a list of strings, where each string may represent any subclass of Number.

How do I parse a string to a generic number, rather than something specific like an integer or float?

解决方案

Number cannot be instantiated because it is an abstract class. I would recommend passing in Numbers, but if you are set on Strings you can parse them using any of the subclasses,

Number num = Integer.parseInt(myString);

or

Number num = NumberFormat.getInstance().parse(myNumber);

@See NumberFormat

这篇关于将字符串解析为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆