fork(4) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.math.BigDecimal;
  7. import java.math.RoundingMode;
  8. import java.text.NumberFormat;
  9. import java.text.ParseException;
  10. import java.util.Locale;
  11.  
  12. class Ideone
  13. {
  14. public static void main (String[] args) throws java.lang.Exception
  15. {
  16. String numero = "199,99";
  17. BigDecimal bg = new BigDecimal(converte(numero)).setScale(2, RoundingMode.HALF_EVEN);
  18. System.out.println(bg);
  19. }
  20.  
  21. public static double converte(String arg) throws ParseException{
  22. //obtem um NumberFormat para o Locale default (BR)
  23. NumberFormat nf = NumberFormat.getNumberInstance(new Locale("pt", "BR"));
  24. //converte um número com vírgulas ex: 2,56 para double
  25. double number = nf.parse(arg).doubleValue();
  26. return number;
  27. }
  28. }
Success #stdin #stdout 0.14s 321280KB
stdin
Standard input is empty
stdout
199.99