Referring to taking a part of a String, you can use a Substring for this. We will also exchange the comma for a point to work in float and perform the desired calculation.
would look this way:
public static void main(String []args){
String preco= "R$20,00";
int um;
int dois;
//pega numeros a esquerda da virgula. . .
um = preco.indexOf("$");
dois = preco.lastIndexOf(",");
String precoX = preco.substring(um + 1, dois);
System.out.println("precoFinal" + precoX.toString());
//Numeros a direita da Virgula. . .
um = preco.indexOf(",");
dois = preco.lastIndexOf(preco.charAt(preco.length() - 1));
String precoY = preco.substring(um + 1, dois + 1);
System.out.println("precoFinal" + precoY.toString());
//Preco pronto para ser convertido. . .
String precoFinal = OrganizaNumero(precoX, precoY, ".");//até esta etapa, coloquei o ponto no lugar da virgula e tirei o R$ da string. . .
//converte precoFinal pra float. . .
float f = Float.parseFloat(precoFinal.toString());
//soma com outro float. . .
f = f + 30.50f;
//transforma novamente em String. . .
String valorSomado = Float.toString(getFloat(f));
System.out.println("string" + valorSomado.toString());
}
public static float getFloat(float x){return x;}
public static String organizaNumero(String x, String y, String z){return "" + x.toString() + z.toString() + y.toString();}
See these links, they answer the two questions: How to retrieve specific parts/values/os from a string? and Convert String to Float with Java comma
– user28595
Tip, use the survey, almost all the most basic questions have answered here on the site same. :)
– user28595