Reputation: 392
i have an issue and will be appreciated if u help me .
this is an example of my issue :
I have some variables like (10.000 , 20,000) that user can select them and increase there credit .
when user click and chose one of them it goes to bank gateway and after payment it goes back to my app .
I used a text view to get the amount . it's no problem with this .
the amount that stored and show to user is 10000 or 20000 or 100000 and i want to be like this 10,000 or 20,000 or 100,000
actually every three number put a comma .
code :
private fun makepayment(amount: Long) {
val progress = findViewById<RelativeLayout>(R.id.rl_progress)
val purchase = ZarinPal.getPurchase(this)
val payment: PaymentRequest = ZarinPal.getSandboxPaymentRequest()
payment.merchantID = getString(R.string.MerchantId)
payment.amount = amount
payment.isZarinGateEnable(true)
payment.description = getString(R.string.incresemoney)
payment.setCallbackURL("returnfromzarinpal://mivebaranapp")
purchase.startPayment(payment) {
status, authority, paymentGatewayUri, intent ->
if (status == 100) {
progress.visibility = View.GONE
startActivity(intent)
} else {
Toast.makeText(
this,
"خطایی در برقراری ارتباط با درگاه رخ داده ، لطفا دوباره امتحان کنید",
Toast.LENGTH_LONG
).show()
}
}
}
private fun paymentVerification() {
val textmoney: TextView = findViewById(R.id.money)
val data: Uri? = intent.data
val getpurchase = ZarinPal.getPurchase(this)
getpurchase.verificationPayment(data) {
isPaymentSuccess, refID, paymentRequest ->
if (isPaymentSuccess) {
textmoney.text = "${paymentRequest.amount} تومان "
Toast.makeText(this, "افزایش موجودی شما با موفقیت انجام شد ", Toast.LENGTH_LONG)
.show()
} else {
Toast.makeText(
this,
"در عملیات پرداخت خطایی رخ داده ، لطفا دوباره سعی کنید",
Toast.LENGTH_LONG
).show()
}
}
Upvotes: 2
Views: 1567
Reputation: 95
Here is my solution:
fun stylePrice(oldPrice: String): String {
if (oldPrice.length > 3) {
val reversed = oldPrice.reversed()
var newPrice = ""
for (i in oldPrice.indices) {
newPrice += reversed[i]
if ((i+1) % 3 == 0) {
newPrice += ','
}
}
val readyToGo = newPrice.reversed()
// if you don't use it, when the number is (for example) 532,234
// it will be like this: ,532,234
// so we need it :-)
if (readyToGo.first() == ',') {
return readyToGo.substring(1) + " Dollars"
}
return "$readyToGo Dollars"
}
return "$oldPrice Dollars"
}
Samples of outputs: 2,000,000
or 2,000
Upvotes: 0
Reputation: 386
You can pass the amount as int to the function as an agrument and returns formatted string as per the requirement. As a separate funtion, it would be useful even if you change pattern in future and can be useful in multiple places.
fun applyPattern(d: Int): String? {
val numberFormat = NumberFormat.getInstance(Locale.US) as DecimalFormat
numberFormat.applyPattern("#,###")
return numberFormat.format(d)
}
Upvotes: 1
Reputation: 4327
Try this :
textView.text = "%,d".format(100000)
Output :
100,000
Upvotes: 2