currencyFormat function

dynamic currencyFormat({
  1. required String data,
  2. required TextEditingController controller,
})

Implementation

currencyFormat({required String data, required TextEditingController controller}) {
  int getLength = data.replaceAll('.', '').length;
  data = data.replaceAll('.', '');
  int tousand = 0;
  String build = '';

  while (getLength > 3) {
    getLength -= 3;
    ++tousand;
    if (tousand == 1) {
      build = '${data.substring(getLength, getLength + 3)}$build';
    } else {
      build = '${data.substring(getLength, getLength + 3)}.$build';
    }
  }
  if (data.length < 4) {
    build = data;
  } else {
    build = '${data.substring(0, getLength)}.$build';
  }
  controller.text = build;
  controller.selection = TextSelection.fromPosition(TextPosition(offset: controller.text.length));
}