Minha duvida é o seguinte, tenho uma String A = “2.685,20” e outra String B= “1.130,50”, como faço pra somar esses dois valores? tenho q trasformar eles pra q tipo de variavel antes da soma?
Obrigado
Minha duvida é o seguinte, tenho uma String A = “2.685,20” e outra String B= “1.130,50”, como faço pra somar esses dois valores? tenho q trasformar eles pra q tipo de variavel antes da soma?
Obrigado
sim senhor
pois se vc fizer .;…
String a = "123";
String b = "321"; //corrigido
String ab = a + b;
O Resultado será “123321” …
Ai para somar:
Integer a = Integer.parseInt(a);
Integer b = Integer.parseInt(b);
Integer abI = a + b;
Se usar Float ou Double … é a mesma apenas substitua o Integer por Float ou Double…
espero ter ajudado !
Transforme para double,
Neste caso vc terá que “parsear” esse números:
DecimalFormat df = new DecimalFormat("###,###,##0.00");
String a = "2.685,20";
String b = "1.130,50";
try {
double d1 = df.parse(a).doubleValue();
double d2 = df.parse(b).doubleValue();
double d3 = d1 + d2;
System.out.println(d3);
String c = df.format(d3);
System.out.println(c);
} catch (ParseException e) {
e.printStackTrace();
}
Valeu cara!! ajudou muito!!!
import java.text.*;
import java.util.*;
class TesteMoeda {
public static void main(String[] args) {
String s1 = "2.685,20";
String s2 = "1.130,50";
Locale brasil = new Locale ("pt", "BR");
NumberFormat nf = new DecimalFormat ("#,##0.00", new DecimalFormatSymbols (brasil));
try {
double d1 = nf.parse (s1).doubleValue();
double d2 = nf.parse (s2).doubleValue();
double soma = d1 + d2;
String s3 = nf.format (soma);
System.out.println (s3);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}