我正在尝试将字符串 pprice 转换为 float 。但是,对象的价格属性( float pt 类型)设置为 0.00。有人可以告诉我哪里出了问题吗?
String pprice="60.0"
String tokens[]=pprice.split(".");
if(tokens.length>=2)
{
int a=Integer.parseInt(tokens[0]);
int b=Integer.parseInt(tokens[1]);
float a1=(float)a;
float b1=(float)b;
Float price=a1+(b1/100);
prod.setProductPrice(price);
}
else if(tokens.length==1)
{
int a=Integer.parseInt(tokens[0]);
float a1=(float)a;
prod.setProductPrice(a1);
}
最佳答案
使用 Double.parseDouble(string) 或 Float.parseFloat(string);
关于java - 在java中将字符串转换为 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9417009/