Convert float or double into real $

9

I have float = 11.6 want to convert into Brazilian currency R$ 11,60 , does anyone know how to do?

    
asked by anonymous 22.04.2016 / 05:16

2 answers

14

I usually use the getCurrencyInstance with locale set manually because when I do not set it it picks up the operating system time and can certainly do a wrong conversion from real to dollar for example.

Double d = 10.1;
Locale ptBr = new Locale("pt", "BR");
String valorString = NumberFormat.getCurrencyInstance(ptBr).format(d);
System.out.println(valorString);
    
22.04.2016 / 13:57
6

There are a few ways to do it, for example:

BigDecimal valor = new BigDecimal ("12000000.12");  
NumberFormat nf = NumberFormat.getCurrencyInstance();  
String formatado = nf.format (valor);
System.out.println(formatado);
//O resultado é R$ 12.000.000,12

Another option:

Long a = Long.parseLong("999999999999999999");
System.out.println(NumberFormat.getCurrencyInstance().format(a));
//saída R$ 999.999.999.999.999.999,00
    
22.04.2016 / 05:28