Skip to content

Commit

Permalink
number utils
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadulislam committed Dec 1, 2022
1 parent 668ea94 commit ba91ec3
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
28 changes: 28 additions & 0 deletions androidutils/src/main/java/com/xihad/androidutils/AndroidUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class AndroidUtils private constructor() {

fun getPaymentUtils(): PaymentUtils = PaymentUtils

fun getNumberUtils(): NumberUtils = NumberUtils


/**
* Quick Access methods
Expand Down Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }


}
126 changes: 126 additions & 0 deletions androidutils/src/main/java/com/xihad/androidutils/utils/NumberUtils.kt
Original file line number Diff line number Diff line change
@@ -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<Char, Char> = 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
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.xihad.androidutils.view

object ViewUtil {




}

0 comments on commit ba91ec3

Please sign in to comment.