Reputation: 1034
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? How can i do that ? I tried this but it won't let me
totaluri[l]=Float.parseFloat(cant[l]) *Float.parseFloat(pret[l]);
Upvotes: 0
Views: 213
Reputation: 109623
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. 5000 * 0.2 will not be 1000.0.
Upvotes: 0
Reputation: 83597
You need to use Double.parseDouble()
or Float.parseDouble()
to convert a String
to a numerical value with a decimal fraction. You can then use Double.toString()
or Float.toString()
to convert the result of your calculation back to a `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.
Upvotes: 2
Reputation: 3223
What about this?
totaluri[l] = new String(Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]));
Upvotes: -1