Skip to content

Instantly share code, notes, and snippets.

@mohamedagamy
Last active October 13, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.
Save mohamedagamy/ada3e2f8e1fb87f8c28f8e36aafa62ab to your computer and use it in GitHub Desktop.
fun String.formatCreditCardNumber(): String {
val delimiter = ' '
return this.replace(".{4}(?!$)".toRegex(), "$0$delimiter")
}
fun Float.formatPriceFloat(): String {
val isEnglish = Utils.getLang()
val symbols = DecimalFormatSymbols(Locale.US)
val decimalFormat = DecimalFormat("#,###.000",symbols)
val output = if(isEnglish) decimalFormat.format(this) else
decimalFormat.format(this).convertEnglishNumbersToArabic()
return output
}
fun Float.formatPriceInt(): String {
val isEnglish = Utils.getLang()
val symbols = DecimalFormatSymbols(Locale.US)
val decimalFormat = DecimalFormat("#,###",symbols)
val output = if(isEnglish) decimalFormat.format(this.toInt()) else
decimalFormat.format(this.toInt()).convertEnglishNumbersToArabic()
return output
}
fun String.formatStringLocalized(): String {
val isEnglish = Utils.getLang()
val input = this.formatStringEnglish()
if (isEnglish) return input
else return input.convertEnglishNumbersToArabic()
}
//you need to use this workaround to avoid arabic comma in Numbers
//so you replace all english numbers with arabic numbers
//https://stackoverflow.com/questions/34266789/arabic-number-in-arabic-text-in-android
fun String.convertEnglishNumbersToArabic(): String {
return this.replace("1".toRegex(), "١").replace("2".toRegex(), "٢")
.replace("3".toRegex(), "٣").replace("4".toRegex(), "٤")
.replace("5".toRegex(), "٥").replace("6".toRegex(), "٦")
.replace("7".toRegex(), "٧").replace("8".toRegex(), "٨")
.replace("9".toRegex(), "٩").replace("0".toRegex(), "٠")
}
private fun String.formatStringEnglish(): String {
val decimalFormat = NumberFormat.getNumberInstance(Locale.ENGLISH)
val result = decimalFormat.format(this.toInt())
return result
}
//*************************************************************
fun String.textOnly(): Boolean {
//not allowed number arabic/english ,not allowed special chars
return this.matches("^[^0-9\u0660-\u0669^~|<>{}¥€£@#&*+=()_\$\";.?!%]+$".toRegex())
}
fun String.digitsOnly(): Boolean {
//arabic,english numbers only
return this.matches("^[0-9\u0660-\u0669]+\$".toRegex())
}
fun String.alphaNumericOnly(): Boolean {
//arabic & english => numbers & characters also no spaces allowed
//this.matches("^(?=.*[\\u0660-\\u0669])(?=.*[\\u0621-\\u064A0]).{6,}\$".toRegex())
return this.matches("^(?=.*[0-9])(?=.*[a-zA-Z]).{6,}\$".toRegex())
}
fun String.isArabicChars(): Boolean {
//arabic characters and numbers
return this.matches("^[\\u0621-\\u064A\\u0660-\\u0669 ]+\$".toRegex())
}
fun String.encodeBase64(): String {
return Base64.encodeToString(toByteArray(), Base64.NO_WRAP)
}
fun String.decodeBase64(): String {
return Base64.decode(this.toByteArray(), Base64.NO_WRAP).toString()
}
@SuppressLint("SimpleDateFormat")
fun calcDiffInDays(startDate: String, endDate: String): Long {
//"2022-12-21 10:58:15"
val dateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
var startPackageDate: Long = 0
var endPackageDate: Long = 0
try {
startPackageDate = dateFormat.parse(startDate)?.time ?: 0
endPackageDate = dateFormat.parse(endDate)?.time ?: 0
} catch (e: Exception) {
e.printStackTrace()
}
var diffInMs = endPackageDate - startPackageDate
diffInMs = if (diffInMs < 0) 0 else diffInMs
val diffInDays = TimeUnit.MILLISECONDS.toDays(diffInMs)
val diffInHours = TimeUnit.MILLISECONDS.toHours(diffInMs)
val diffInMin = TimeUnit.MILLISECONDS.toMinutes(diffInMs)
val diffInSec = TimeUnit.MILLISECONDS.toSeconds(diffInMs)
val totalDifference = Math.ceil(diffInSec.toDouble() / (60.0 * 60.0 * 24.0))
Log.e("TIMER", "diffTotal $totalDifference")
Log.e("TIMER", "diffSec $diffInSec")
return diffInDays
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment