translateAmountToLaoWords static method

String translateAmountToLaoWords(
  1. String numberToTranslate
)

Implementation

static String translateAmountToLaoWords(String numberToTranslate) {
  int number = 0;
  int decimal = 0;
  bool applyThoundsandBilion = false;

  if (numberToTranslate.contains('.')) {
    number = int.parse(
        numberToTranslate.split('.')[0].toString().replaceAll(',', ''));

    decimal = int.parse(
        numberToTranslate.split('.')[1].toString().replaceAll(',', ''));
  } else {
    number = int.parse(numberToTranslate.replaceAll(',', ''));
  }

  if (number == 0) {
    return 'ສູນ';
  }

  final List<String> units = [
    '',
    'ໜຶ່ງ',
    'ສອງ',
    'ສາມ',
    'ສີ່',
    'ຫ້າ',
    'ຫົກ',
    'ເຈັດ',
    'ແປດ',
    'ເກົົ້າ',
    'ສິບ',
    'ສິບເອັດ',
    'ສິບສອງ',
    'ສິບສາມ',
    'ສິບສີ່',
    'ສິບຫ້າ',
    'ສິບຫົກ',
    'ສິບເຈັດ',
    'ສິບແປດ',
    'ສິບເກົ້າ'
  ];

  final List<String> tens = [
    '',
    '',
    'ຊາວ',
    'ສາມສິບ',
    'ສີ່ສິບ',
    'ຫ້າສີບ',
    'ຫົກສິບ',
    'ເຈັດສິບ',
    'ແປດສິບ',
    'ເກົ້າສິບ'
  ];

  final List<String> powersOfTen = ['', 'ພັນ', 'ລ້ານ', 'ຕື້', 'ພັນ'];

  List<String> result = [];
  int powerIndex = 0;
  bool applyPowerTen = true;
  bool applyEdd = false;
  bool zeroChunk = false;

  while (number > 0) {
    int chunk = number % 1000;

    zeroChunk = chunk == 0;

    if (powerIndex == 3 && zeroChunk) {
      applyThoundsandBilion = true;
    }

    if (chunk.toString().length == 2 &&
        chunk.toString().substring(0, 1) != "0") {
      applyEdd = true;
    } else if (chunk.toString().length == 3 &&
        chunk.toString().substring(1, 2) != "0") {
      applyEdd = true;
    } else {
      applyEdd = false;
    }

    if (chunk != 0) {
      List<String> chunkWords = [];

      if (chunk >= 100) {
        switch (powerIndex) {
          case 0:
            chunkWords.add('${units[chunk ~/ 100]} ຮ້ອຍ');
            chunk %= 100;
            break;

          case 1:
            chunkWords.add('${units[chunk ~/ 100]} ແສນ');
            chunk %= 100;
            break;
          default:
            chunkWords.add('${units[chunk ~/ 100]} ຮ້ອຍ');
            chunk %= 100;
        }
      }
      if (chunk >= 20) {
        switch (powerIndex) {
          case 0:
            chunkWords.add(tens[chunk ~/ 10]);
            chunk %= 10;
            break;

          case 1:
            chunkWords.add('${units[chunk ~/ 10]} ໝື່ນ');
            chunk %= 10;
            break;
          default:
            chunkWords.add(tens[chunk ~/ 10]);
            chunk %= 10;
        }
      }

      if (chunk > 0) {
        if (chunk < 20) {
          if (chunk == 1 && applyEdd) {
            chunkWords.add('ເອັດ');
          } else {
            switch (powerIndex) {
              case 0:
                if (chunk == 1) {
                  chunkWords.add('ໜຶ່ງ');
                } else {
                  chunkWords.add(units[chunk]);
                }

                break;

              case 1:
                chunkWords.add(units[chunk]);
                break;
              default:
                chunkWords.add(units[chunk]);
            }
          }
        } else {
          chunkWords.add(tens[chunk ~/ 10]);
          chunk %= 10;
          if (chunk > 0) {
            chunkWords.add(units[chunk]);
          }
        }
      } else {
        if (powerIndex > 1) {
          applyPowerTen = true;
        } else {
          applyPowerTen = false;
        }
      }
      // Join the words for this chunk and add them to the result list
      result.insert(0, chunkWords.join(' '));
    }
    // Add the power of ten (e.g., thousand, million) to the result if necessary
    if (powerIndex > 0 && result.isNotEmpty && applyPowerTen && !zeroChunk) {
      result.insert(1, powersOfTen[powerIndex]);
    }

    // Move to the next group of three digits
    number ~/= 1000;
    applyPowerTen = true;
    powerIndex++;
  }

  //if number in range 1,000,000,000,000
  if (applyThoundsandBilion) result.insert(2, powersOfTen[3]);

  //translate decimal
  var decimalResult = _decimalPointTranslateLao(decimal.toString());

  // Join the words in the result list with spaces to form the final representation
  if (decimalResult != "" && decimalResult != "ສູນ") {
    return "${result.join(' ')} ຈຸດ $decimalResult";
  } else {
    return result.join(' ');
  }
}