/**
* Return the float receiver as a string display with numOfDec after the decimal (rounded)
* (e.g. 35.72 with numOfDec = 1 will be 35.7, 35.78 with numOfDec = 2 will be 35.80)
*
* @param numOfDec number of decimal places to show (receiver is rounded to that number)
* @return the String representation of the receiver up to numOfDec decimal places
*/
fun Float.toString(numOfDec: Int): String {
val integerDigits = this.toInt()
val floatDigits = ((this - integerDigits) * 10f.pow(numOfDec)).roundToInt()
return "${integerDigits}.${floatDigits}"
}
try {
if (!TextUtils.isDigitsOnly(st)) {
val rounded = Math.round(st.toFloat())
val toHex = BigInteger(rounded.toString(), 10)
val t = toHex.toString(16)
t.toString()
} else {
val toHex = BigInteger(st, 10)
val t = toHex.toString(16)
t.toString()
}
} catch (e: NumberFormatException) {
error
}
3条答案
按热度按时间tzdcorbm1#
我创建了下面的Float扩展(这也应该可以用于Double):
kdfy810k2#
在您的方案中使用此选项
kiayqfof3#
如果你想返回一个浮点数,但只想去掉小数点后的小数,那么就使用下面的代码: