flutterdartcurrencynumber-formattingflutter-intl

not able to solve number format in currency?


i have tried NumberFormat "#,##0.##"

i have tried NumberFormat "#,##0.00"

i have tried NumberFormat "#,##0.#0"

the format i want is (123,4567.89)

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';

class CurrencyFormat extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    // TODO: implement formatEditUpdate
    if (newValue.selection.baseOffset == 0) {
      return newValue;
    }
    double value = double.parse(newValue.text);
    final money = NumberFormat("#,###.##", "en_US");

    String newText = money.format(value);

    return newValue.copyWith(
        text: newText,
        selection: TextSelection.collapsed(offset: newText.length));
  }
}

i am calling this Currency format in a textformfield widget

'''
Widget mrpTextField() {
    return Padding(
      padding: const EdgeInsets.all(8.0),
      child: TextFormField(
        keyboardType: TextInputType.number,
        inputFormatters: [
          FilteringTextInputFormatter.digitsOnly,
          CurrencyFormat()
        ],
        controller: _mrp,
        validator: (value) {
          if (value.isEmpty) {
            return "MRP";
          }else if (value.length > 6) {
            return "";
          }
          return null;
        },
        decoration: InputDecoration(
          labelText: "Enter MRP",
        ),
        maxLines: null,
      ),
    );
  }
'''

Solution

  • In TextFormField change FilteringTextInputFormatter.digitsOnly, to FilteringTextInputFormatter.allow(RegExp("[0-9\.]")),

    In CurrencyFormat

      class CurrencyFormat extends TextInputFormatter {
      @override
      TextEditingValue formatEditUpdate(
          TextEditingValue oldValue, TextEditingValue newValue) {
        if (newValue.text.isEmpty) {
          return newValue.copyWith(text: '');
        } else if (newValue.text == ".") {
          return newValue.copyWith(
            text: '0.00',
            selection: TextSelection.collapsed(offset: 2),
          );
        } else {
          NumberFormat f = NumberFormat("#,###0.00", "en_US");
          double newNumber = 0;
          if ((!newValue.text.contains(".")) && oldValue.text.contains('.')) {
            String tempString = newValue.text.replaceAll(f.symbols.GROUP_SEP, '');
            tempString = tempString.substring(0, tempString.length - 2) +
                "." +
                tempString.substring(tempString.length - 2);
            newNumber = double.parse(tempString);
          } else {
            newNumber = double.parse(newValue.text
                .replaceAll(f.symbols.GROUP_SEP, '')
                .replaceAll("..", '.'));
          }
          String newString = f.format(newNumber);
          int cursorPosition = 0;
          if (oldValue.text.length > newString.length) {
            cursorPosition = -1;
          } else if (oldValue.text.length < newString.length) {
            cursorPosition = 1;
          } else {
            if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length >
                newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
              cursorPosition = -1;
              if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
                newString = "";
              }
            } else if (oldValue.text.replaceAll(f.symbols.GROUP_SEP, '').length <
                newValue.text.replaceAll(f.symbols.GROUP_SEP, '').length) {
              cursorPosition = 1;
            } else if (oldValue.selection.extentOffset >
                oldValue.selection.baseOffset) {
              cursorPosition =
                  oldValue.selection.baseOffset - oldValue.selection.extentOffset;
              newString =
                  newString.substring(0, oldValue.selection.baseOffset - 1) +
                      newString.substring(oldValue.selection.baseOffset + 1);
              newNumber = double.parse(newString
                  .replaceAll(f.symbols.GROUP_SEP, '')
                  .replaceAll("..", '.'));
              newString = f.format(newNumber);
              if (newString == "0.00" && oldValue.selection.baseOffset == 0) {
                newString = "";
              }
            }
          }
          return TextEditingValue(
            text: newString,
            selection: TextSelection.collapsed(
                offset: oldValue.selection.extent.offset +
                    cursorPosition +
                    (f.symbols.GROUP_SEP.allMatches(newString).length -
                        f.symbols.GROUP_SEP.allMatches(oldValue.text).length)),
          );
        }
      }
    }
    

    Again, "#,###0.00" is right