Remove scientific notation formatting for Float values in kotlin android-ThrowExceptions

Exception or error:

I am trying to set the float value to an edit text. For this I am using a binding adapter like this below.

@BindingAdapter("android:text")
@JvmStatic
fun setAmount(editText: EditText, currency: Float?) {
    if (currency!= null && currency!=0.0f) {

       editText.setText(""+Math.round(currency))
    }else{
        editText.setText("")
    }
}

Model Class

class OpportunityModel : BaseObservable(), Serializable {

var accountName = ""
var accountGuid: String? = null
var amount = 0.0
var potentialAmount:Float = 0.0f
get() = field
set(value) {field=value}
var contactPersonName = ""
var fieldOne = ""
var fieldTwo = ""
var fieldThree = ""
var fieldFour = ""
var fieldFive = ""
var opportunityName = ""
var opportunityGuid: String? = null
var opportunityRating = 0
var opportunityReasonGuid: String? = null
var opportunityIntStatus = 2
var opportunityDispStatus = ""
var opportunityNotAvailable = false
var genericFieldUI: GenericFieldDto? = null
@SerializedName("expDateOfClosure")
var dateForServer: String? = null
var expDate = ""
var contactPersonNameGuid: String? = null
var listOfAccountContact = ArrayList<AccountContactPersonModel>()
var listOfReasonMaster = ArrayList<ReasonMasterDto>()}

This shows the value properly in the edit text but when this value is added to the model class via data binding, it gets converted to scientific notation and is showing values like 1E+07. How can I stop this conversion to scientific notation ?

How to solve:

You can use String.format, like

@BindingAdapter("android:text")
@JvmStatic
fun setAmount(editText: EditText, currency: Float?) {
    if (currency!= null && currency!=0.0f) {

       editText.setText(String.format("%.8f", currency))
    }else{
        editText.setText("")
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *