This is what I am doing to display the amount with two decimal places. It is working fine, but I would like to know if this is right or is there any drawback in doing so and what is the better way to do it?
I want to format the amount for 'Your Price' and 'MRP'. How can I do that?
holder.itemView.tv_dashboard_item_title.text = model.title
holder.itemView.tv_dashboard_item_price.text = "Your Price ₹${model.price}"
holder.itemView.tv_dashboard_item_mrp.text = "MRP ₹${model.mrp}"
val mrp:Double? = model.mrp
val price: Double? = model.price
val save=DecimalFormat("#,##0.00")
val save2: Double = mrp!! - price!!
val saves=save.format(save2)
holder.itemView.tv_dashboard_item_you_save.text = "You Save ₹ $saves"
Thank you.
EDIT
Revised code.
val decFormat = DecimalFormat("#,##0.00")
holder.itemView.tv_dashboard_item_title.text = model.title
val mrp: BigDecimal = model.mrp.toBigDecimal()
val price: BigDecimal = model.price.toBigDecimal()
val save: BigDecimal = mrp - price
val saveAmount = decFormat.format(save)
holder.itemView.tv_dashboard_item_price.text = decFormat.format(price)
holder.itemView.tv_dashboard_item_mrp.text = decFormat.format(mrp)
holder.itemView.tv_dashboard_item_you_save.text = "You Save ₹ $saveAmount"
EDIT 2
Following is the model class Product.kt
@Parcelize
data class Product(
val user_id: String = "",
val user_name: String = "",
val title: String = "",
val mrp: BigDecimal= "0.00".toBigDecimal(),
val price: BigDecimal = "0.00".toBigDecimal(),
val description: String = "",
val stock_quantity: String = "",
val image: String = "",
var brand_name:String="",
var manufacturer:String="",
var main_category:String="",
var sub_category:String="",
var product_id: String = "",
) : Parcelable
Function in AddProductActivity.kt
to upload product details
private fun uploadProductDetails() {
val username =
this.getSharedPreferences(Constants.TRAD_PREFERENCES, Context.MODE_PRIVATE)
.getString(Constants.LOGGED_IN_USERNAME, "")!!
val product = Product(
FirestoreClass().getCurrentUserID(),
username,
et_product_title.text.toString().trim { it <= ' ' },
et_product_mrp.text.toString().toBigDecimal(),
et_product_price.text.toString().toBigDecimal(),
et_product_description.text.toString().trim { it <= ' ' },
et_product_quantity.text.toString().trim { it <= ' ' },
mProductImageURL,
et_product_brand_name.text.toString().trim { it <= ' ' },
et_product_manufacturer.text.toString().trim { it <= ' ' },
et_product_main_category.text.toString().trim { it <= ' ' },
et_product_sub_category.text.toString().trim { it <= ' ' },
)
FirestoreClass().uploadProductDetails(this@AddProductActivity, product)
}
Logcat
--------- beginning of crash
2021-06-19 18:36:11.605 6674-6674/com.trad E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.trad, PID: 6674
java.lang.IllegalArgumentException: Could not serialize object. Numbers of type BigDecimal are not supported, please use an int, long, float or double (found in field 'mrp')
at com.google.firebase.firestore.util.CustomClassMapper.serializeError(CustomClassMapper.java:555)
at com.google.firebase.firestore.util.CustomClassMapper.serialize(CustomClassMapper.java:122)
at com.google.firebase.firestore.util.CustomClassMapper.access$400(CustomClassMapper.java:54)
at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.serialize(CustomClassMapper.java:902)
at com.google.firebase.firestore.util.CustomClassMapper.serialize(CustomClassMapper.java:178)
at com.google.firebase.firestore.util.CustomClassMapper.serialize(CustomClassMapper.java:104)
at com.google.firebase.firestore.util.CustomClassMapper.convertToPlainJavaTypes(CustomClassMapper.java:78)
at com.google.firebase.firestore.UserDataReader.convertAndParseDocumentData(UserDataReader.java:231)
at com.google.firebase.firestore.UserDataReader.parseMergeData(UserDataReader.java:87)
at com.google.firebase.firestore.DocumentReference.set(DocumentReference.java:166)
at com.trad.firestore.FirestoreClass.uploadProductDetails(FirestoreClass.kt:246)
at com.trad.ui.activities.AddProductActivity.uploadProductDetails(AddProductActivity.kt:280)
at com.trad.ui.activities.AddProductActivity.imageUploadSuccess(AddProductActivity.kt:254)
at com.trad.firestore.FirestoreClass$uploadImageToCloudStorage$1$1.onSuccess(FirestoreClass.kt:212)
at com.trad.firestore.FirestoreClass$uploadImageToCloudStorage$1$1.onSuccess(FirestoreClass.kt:27)
at com.google.android.gms.tasks.zzn.run(com.google.android.gms:play-services-tasks@@17.2.0:4)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8512)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)