obterReal static method

String obterReal(
  1. double value, {
  2. bool moeda = true,
  3. int decimal = 2,
})

Retorna o número real informado, utilizando a máscara: R$ 50.000,00 ou 50.000,00

Implementation

static String obterReal(double value, {bool moeda = true, int decimal = 2}) {
  bool isNegative = false;

  if (value.isNegative) {
    isNegative = true;
    value = value * (-1);
  }

  String fixed = value.toStringAsFixed(decimal);
  List<String> separatedValues = fixed.split(".");

  separatedValues[0] = adicionarSeparador(separatedValues[0]);
  String formatted = separatedValues.join(",");

  if (isNegative) {
    formatted = "-$formatted";
  }

  if (moeda) {
    return r"R$ " + formatted;
  } else {
    return formatted;
  }
}