Friday, June 7, 2013

[android help] Multiply strings?


Multiply strings?


java - Multiply strings? - Stack Overflow







Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















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]);




























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 Strings and Java's primitive data types.



























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.






















What about this?



totaluri[l] = new String(Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]));



















lang-java






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...