首页 > 解决方案 > MutableLiveData 长到十进制

问题描述

如何将金额从 1000 转换为 10.00?

private MutableLiveData<Long> amt = new MutableLiveData<>();

public void setAmt(long value) {
    amt.postValue(value);
}

public LiveData<Long> getAmt() {
    return amt;
}

public LiveData<Double> getDecimalAmt() {
  // How to convert long to decimal?
  // (amt / 100)
}

标签: androidandroid-livedata

解决方案


你可以这样做:

Java代码:

MutableLiveData<Long> longValue = new MutableLiveData()
LiveData<Double> getDecimalAmt(){
        return Transformations.map(longValue) { (double)(it /100) }
    }

科特林代码:

var longValue: MutableLiveData<Long> = MutableLiveData()
fun getDecimalAmt(): LiveData<Double> = Transformations.map(longValue) { it.div(100).toDouble() }

推荐阅读