[英]Multiply strings?
So,i have those : 所以,我有那些:
String[] pret = new String[allcant.size()];
String[] totaluri = new String[allcant.size()];
String[] cant = new String[allcant.size()];
I want to do something like this : 我想做这样的事情:
totaluri[l]=pret[l]*cant[l];`
but i can't.I guess i have to make them float? 但是我不能。我想我必须让它们漂浮吗? since my input from the edittexts that get the values in the cant and pret are decimals? 因为我从edittext中获取的值的值是小数点? How can i do that ? 我怎样才能做到这一点 ? I tried this but it won't let me 我尝试了这个,但它不会让我
totaluri[l]=Float.parseFloat(cant[l]) *Float.parseFloat(pret[l]);
You need to use Double.parseDouble()
or Float.parseDouble()
to convert a String
to a numerical value with a decimal fraction. 您需要使用Double.parseDouble()
或Float.parseDouble()
将String
转换为带小数的数值。 You can then use Double.toString()
or Float.toString()
to convert the result of your calculation back to a `String. 然后,您可以使用Double.toString()
或Float.toString()
将计算结果转换回`String。
Putting this all together: 全部放在一起:
double temp = Double.parseDouble(cant[l]) * Double.parseDouble(pret[l]);
totaluri[l] = Double.toString(temp);
I strongly suggest that you read Primitive Data Types and Lesson: Numbers and Strings from Oracle's Java Tutorial for more information about using String
s and Java's primitive data types. 我强烈建议您从Oracle Java教程中阅读“ 原始数据类型和课程:数字和字符串” ,以获取有关使用String
和Java原始数据类型的更多信息。
BigDecimal pretBD = BigDecimal.valueOf(pret[i]);
BigDecimal cantBD = BigDecimal.valueOf(cant[i]);
BigDecimal totaluriBD = pretBD.multiply(cantBD).setScale(2); // Decimals #.##
totaluri[i] = totaluriDB.toString();
It is much typing, and no operators (multiply, add), but double
is for financial purposes inadequate. 它的类型很多,并且没有运算符(相乘,相加),但是出于财务目的, double
是不够的。 5000 * 0.2 will not be 1000.0. 5000 * 0.2将不是1000.0。
那这个呢?
totaluri[l] = new String(Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.