Android 可变LiveData长到十进制
如何将金额从1000转换为10.00Android 可变LiveData长到十进制,android,android-livedata,Android,Android Livedata,如何将金额从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> getDeci
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)
}
private MutableLiveData amt=new MutableLiveData();
公共无效设置金额(长值){
金额后价值(价值);
}
公共LiveData getAmt(){
返回金额;
}
公共LiveData getDecimalAmt(){
//如何将long转换为十进制?
//(金额/100)
}
您可以使用以下方法:
Java代码:
MutableLiveData<Long> longValue = new MutableLiveData()
LiveData<Double> getDecimalAmt(){
return Transformations.map(longValue) { (double)(it /100) }
}
MutableLiveData longValue=new MutableLiveData()
LiveData getDecimalAmt(){
返回转换.map(longValue){(double)(it/100)}
}
Kotlin代码:
var longValue: MutableLiveData<Long> = MutableLiveData()
fun getDecimalAmt(): LiveData<Double> = Transformations.map(longValue) { it.div(100).toDouble() }
var longValue:MutableLiveData=MutableLiveData()
fun getDecimalAmt():LiveData=Transformations.map(longValue){it.div(100.toDouble()}
您可以使用以下方法:
Java代码:
MutableLiveData<Long> longValue = new MutableLiveData()
LiveData<Double> getDecimalAmt(){
return Transformations.map(longValue) { (double)(it /100) }
}
MutableLiveData longValue=new MutableLiveData()
LiveData getDecimalAmt(){
返回转换.map(longValue){(double)(it/100)}
}
Kotlin代码:
var longValue: MutableLiveData<Long> = MutableLiveData()
fun getDecimalAmt(): LiveData<Double> = Transformations.map(longValue) { it.div(100).toDouble() }
var longValue:MutableLiveData=MutableLiveData()
fun getDecimalAmt():LiveData=Transformations.map(longValue){it.div(100.toDouble()}
请参见android.arch.lifecycle.Transformations
请参见android.arch.lifecycle.Transformations
谢谢。java代码有什么错误吗?it从哪里来?@Alvin为什么不直接参考一下?是的,使用java代码,它会给我一些错误。@Alvin你在官方文档中看到了什么错误?@Alvin,那么看看官方文档中的示例吧?你看过吗?谢谢。java代码有什么错误吗?it从哪里来?@Alvin为什么不直接参考一下?是的,使用java代码,它会给我一些错误。@Alvin你在官方文档中看到了什么错误?@Alvin,那么看看官方文档中的示例吧?你看过吗?