使用Double强转后再转为int

eg: String num ="1.00";

int abc =Double.valueOf(num).intValue();//转换为Int类型


java把含小数点的数字字符串转换为int类型


为什么不能直接使用Integer进行强转,是因为 :

public static Integer valueOf(String s) throws  NumberFormatException

//返回初始化为指定 String 值的新的 Integer 对象。若该 String 不能作为 int 分析,则抛出异常。

字符串 “20.0” 是不能作为 int 类型解析的,所以抛出了异常。

相关文章:

  • 2022-12-23
  • 2022-02-07
  • 2022-12-23
  • 2021-11-22
  • 2021-12-23
  • 2021-12-27
  • 2021-12-20
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2023-04-03
  • 2021-05-21
  • 2022-12-23
相关资源
相似解决方案