translateAmountToThaiWords static method

String translateAmountToThaiWords(
  1. String numberToTranslate
)

Implementation

static String translateAmountToThaiWords(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 = _decimalPointTranslate(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(' ');
  }
}