简体   繁体   English

什么是java-(number)d中的类型变量

[英]What is type variable in java -(number)d

I can't cast this value as Integer and I can't file what type is (I know is String ) but I need convert as number, Interger.parse() does not work I get Exception . 我不能将此值转换为Integer ,也无法归档什么类型(我知道是String ),但是我需要将其转换为数字, Interger.parse()不能正常工作,我会得到Exception

String str = "-10d";
Interger.parse(str); //I get Exception

String str = "-10b" (mentioned in question) or "-10d" (mentioned in title)? String str = "-10b" (有问题)或“ -10d”(有标题)?

If what you mean is -(number)d,then it is double. 如果您的意思是-(number)d,则为两倍。
try 尝试

double b = Double.parseDouble("-10d"); 

instead of using Integer.parseInt 而不是使用Integer.parseInt

You can use this to convert your String to int . 您可以使用此方法将String转换为int

String str = "-10d";
int i = Double.valueOf(str).intValue();

Hope this helps! 希望这可以帮助!

Here you get the double value: 在这里,您获得了double值:

String str = "-10d";
Double d = Double.parseDouble(str);
System.out.println(d);

And to get the integer value: 并获得整数值:

int intValue = d.intValue();

Other solutions, which change the input string for the parse method are (I don't recommend them): 其他解决方案会更改parse方法的输入string (我不建议这样做):

int x = Integer.valueOf(str.substring(0, str.length() - 1));
int y = Integer.valueOf(str.replace('d', ' ').trim());

You get Exception because it is not correct there are no Interger.parse(str); 您会收到Exception,因为它不正确,没有Interger.parse(str); i think you want to make : 我想你想做:

Integer.parseInt(str);

But your string have d and d work with Double not with Integer so instead use this : 但是您的字符串中的d和d与Double一起使用而不与Integer一起使用,因此请使用以下命令:

String str = "-10d";
Double.parseDouble(str);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM