From ba91ec3bcc02d8f1db2a4aa63d88127de8afbaa4 Mon Sep 17 00:00:00 2001 From: ziad Date: Thu, 1 Dec 2022 18:38:16 +0600 Subject: [PATCH] number utils --- .../com/xihad/androidutils/AndroidUtils.kt | 28 ++++ .../androidutils/activity/ExampleActivity.kt | 1 + .../xihad/androidutils/utils/CurrencyUtils.kt | 21 +++ .../xihad/androidutils/utils/NumberUtils.kt | 126 ++++++++++++++++++ .../com/xihad/androidutils/view/ViewUtil.kt | 8 ++ 5 files changed, 184 insertions(+) create mode 100644 androidutils/src/main/java/com/xihad/androidutils/utils/CurrencyUtils.kt create mode 100644 androidutils/src/main/java/com/xihad/androidutils/utils/NumberUtils.kt create mode 100644 androidutils/src/main/java/com/xihad/androidutils/view/ViewUtil.kt diff --git a/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt b/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt index c7e730e..de3a467 100644 --- a/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt +++ b/androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt @@ -53,6 +53,8 @@ class AndroidUtils private constructor() { fun getPaymentUtils(): PaymentUtils = PaymentUtils + fun getNumberUtils(): NumberUtils = NumberUtils + /** * Quick Access methods @@ -123,6 +125,32 @@ class AndroidUtils private constructor() { fun stringToNumber(inputNumber: String) = PaymentUtils.stringToNumber(inputNumber) + /** + * CurrencyUtils + * + */ + + fun getCurrencyCode(countryCode: String) = CurrencyUtils.getCurrencyCode(countryCode) + + fun getCurrencySymbol(countryCode: String) = CurrencyUtils.getCurrencySymbol(countryCode) + + fun getCountryCode(countryName: String) = CurrencyUtils.getCountryCode(countryName) + + + /** + * NumberUtils + */ + fun numberToWords(number: Long) = NumberUtils.numberToWords(number) + + fun numberInBangla(number: String) = NumberUtils.numberInBangla(number) + + fun getDigitBanglaFromEnglish(number: String) = + NumberUtils.getDigitBanglaFromEnglish(number) + + fun getDigitEnglishFromBangla(number: String) = + NumberUtils.getDigitEnglishFromBangla(number) + + /** * show a Toast in simple way */ diff --git a/androidutils/src/main/java/com/xihad/androidutils/activity/ExampleActivity.kt b/androidutils/src/main/java/com/xihad/androidutils/activity/ExampleActivity.kt index 4c89d7d..2be0720 100644 --- a/androidutils/src/main/java/com/xihad/androidutils/activity/ExampleActivity.kt +++ b/androidutils/src/main/java/com/xihad/androidutils/activity/ExampleActivity.kt @@ -6,6 +6,7 @@ import com.xihad.androidutils.AndroidUtils import com.xihad.androidutils.R import com.xihad.androidutils.effect.applyClickEffect import com.xihad.androidutils.utils.DebounceUtils +import com.xihad.androidutils.utils.PaymentUtils.twoDigitString import kotlinx.android.synthetic.main.activity_example.* class ExampleActivity : AppCompatActivity() { diff --git a/androidutils/src/main/java/com/xihad/androidutils/utils/CurrencyUtils.kt b/androidutils/src/main/java/com/xihad/androidutils/utils/CurrencyUtils.kt new file mode 100644 index 0000000..4d0c41d --- /dev/null +++ b/androidutils/src/main/java/com/xihad/androidutils/utils/CurrencyUtils.kt @@ -0,0 +1,21 @@ +package com.xihad.androidutils.utils + +import java.util.* + +object CurrencyUtils { + + //to retrieve currency code + fun getCurrencyCode(countryCode: String): String { + return Currency.getInstance(Locale("", countryCode)).currencyCode + } + + //to retrieve currency symbol + fun getCurrencySymbol(countryCode: String): String { + return Currency.getInstance(Locale("", countryCode)).symbol + } + + fun getCountryCode(countryName: String) = + Locale.getISOCountries().find { Locale("", it).displayCountry == countryName } + + +} \ No newline at end of file diff --git a/androidutils/src/main/java/com/xihad/androidutils/utils/NumberUtils.kt b/androidutils/src/main/java/com/xihad/androidutils/utils/NumberUtils.kt new file mode 100644 index 0000000..84ae9cc --- /dev/null +++ b/androidutils/src/main/java/com/xihad/androidutils/utils/NumberUtils.kt @@ -0,0 +1,126 @@ +package com.xihad.androidutils.utils + +object NumberUtils { + + + fun String.enDigitToBn(): String { + val bnDigits = listOf('০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯') + return this.map { bnDigits[it.toString().toInt()] }.joinToString("") + } + + + fun getDigitEnglishFromBangla(number: String): String { + val banglaToEnglishDigitsMap: MutableMap = HashMap() + banglaToEnglishDigitsMap['০'] = '0' + banglaToEnglishDigitsMap['১'] = '1' + banglaToEnglishDigitsMap['২'] = '2' + banglaToEnglishDigitsMap['৩'] = '3' + banglaToEnglishDigitsMap['৪'] = '4' + banglaToEnglishDigitsMap['৫'] = '5' + banglaToEnglishDigitsMap['৬'] = '6' + banglaToEnglishDigitsMap['৭'] = '7' + banglaToEnglishDigitsMap['৮'] = '8' + banglaToEnglishDigitsMap['৯'] = '9' + val builder = java.lang.StringBuilder() + try { + for (i in number.indices) { + if (banglaToEnglishDigitsMap.containsKey(number[i])) { + builder.append(banglaToEnglishDigitsMap[number[i]]) + } else { + builder.append(number[i]) + } + } + } catch (e: java.lang.Exception) { + e.printStackTrace() + } + + return builder.toString() + } + + + fun getDigitBanglaFromEnglish(number: String): String { + val banglaDigits = charArrayOf('০', '১', '২', '৩', '৪', '৫', '৬', '৭', '৮', '৯') + + val builder = StringBuilder() + try { + for (i in number.indices) { + if (Character.isDigit(number[i])) { + if (number[i].toInt() - 48 <= 9) { + builder.append(banglaDigits[number[i].toInt() - 48]) + } else { + builder.append(number[i]) + } + } else { + builder.append(number[i]) + } + } + } catch (e: Exception) { + e.printStackTrace() + } + return builder.toString() + } + + fun numberInBangla(number: String): String { + + return number.replace("0", "০").replace("1", "১").replace("2", "২") + .replace("3", "৩").replace("4", "৪").replace("5", "৫").replace("6", "৬") + .replace("7", "৭").replace("8", "৮").replace("9", "৯") + + } + + + fun numberToWords(number: Long): String { + var limit = 1000000000000L + var currHun: Long + var t: Long = 0 + + if (number == 0L) return "Zero" + + val multiplier = arrayOf("", "Trillion", "Billion", "Million", "Thousand") + + val firstTwenty = arrayOf( + "", "One", "Two", "Three", + "Four", "Five", "Six", "Seven", + "Eight", "Nine", "Ten", "Eleven", + "Twelve", "Thirteen", "Fourteen", "Fifteen", + "Sixteen", "Seventeen", "Eighteen", "Nineteen" + ) + + val tens = arrayOf( + "", "Twenty", "Thirty", + "Forty", "Fifty", "Sixty", + "Seventy", "Eighty", "Ninety" + ) + + if (number < 20L) return firstTwenty[number.toInt()] + var answer = "" + var i = number + while (i > 0) { + currHun = i / limit + + while (currHun == 0L) { + i %= limit + limit /= 1000 + currHun = i / limit + ++t + } + + if (currHun > 99) answer += (firstTwenty[currHun.toInt() / 100] + + " Hundred ") + + currHun %= 100 + + + if (currHun in 1..19) answer += firstTwenty[currHun.toInt()] + " " else if (currHun % 10 == 0L && currHun != 0L) answer += tens[currHun.toInt() / 10 - 1] + " " else if (currHun in 21..99) answer += (tens[currHun.toInt() / 10 - 1] + " " + + firstTwenty[currHun.toInt() % 10] + + " ") + + if (t < 4) answer += multiplier[(++t).toInt()] + " " + i %= limit + limit /= 1000 + } + return answer + } + + +} \ No newline at end of file diff --git a/androidutils/src/main/java/com/xihad/androidutils/view/ViewUtil.kt b/androidutils/src/main/java/com/xihad/androidutils/view/ViewUtil.kt new file mode 100644 index 0000000..1362f63 --- /dev/null +++ b/androidutils/src/main/java/com/xihad/androidutils/view/ViewUtil.kt @@ -0,0 +1,8 @@ +package com.xihad.androidutils.view + +object ViewUtil { + + + + +} \ No newline at end of file