formatToTwoDecimalPlaces(): String = String.format("%.2f", this) val number = 123.4567 val formattedNumber = number.formatToTwoDecimalPlaces() println(formattedNumber) // 输出: 123.46 这种方式让你的代码更加Kotlin风格,并且使数值格式化更加直观和方便。 综上所述,你可以根据自己的需求选择合适的...
.formatDecimalInterruptOrFillMode( decimalPlaces: Int = 2, failValue: Double = 0.0 ): String = try { String.format("%.\({decimalPlaces}f", this.toString().toDouble()) } catch (e: NumberFormatException) { String.format("%.\){decimalPlaces}f", failValue) } /** 强模式小数格式化,超长...
fun main(args: Array<String>) { val num = 1.34567 println("%.4f".format(num)) } When you run the program, the output will be: 1.3457 In the above program, we've used format() method to print the given floating point number num to 4 decimal places. The 4 decimal places is given...
问带2个小数的Kotlin浮点数对字符串没有精度损失EN$f = 0.57; echo intval($f * 100); //56...
println("format:${feature.toLong(DurationUnit.MILLISECONDS)}") } 这样一对比Duration简化了时间单位换算的过程。尤其是较为复杂的时间运算和换算中会让你非常惊喜。一起来学习下kotlin的时间度量api吧。 /创建Duration/ 创建Duration最方便是使用Int, Long, Double的扩展...
3. Using String.format() We can use the format() method from the String class to convert the decimal value to a string format without scientific notation. Let’s look at the code: val num = 0.000123456789 println(num) // prints 1.23456789E-4 val numStr = String.format(Locale.US, "...
项目功能介绍:原本是RxJava2和Retrofit2项目,现已更新使用Kotlin+RxJava2+Retrofit2+MVP架构+组件化和 Kotlin+Retrofit2+协程+Jetpack MVVM架构+组件化,添加自动管理token功能,添加RxJava2生命周期管理,集成极光推送、阿里云Oss对象存储和高德地图定位功能。
notScience(scale: Int = 2): String { val mat = StringBuilder("#") if (scale > 0) { mat.append(".") for (i in 0 until scale) mat.append("#") } val df = DecimalFormat(mat.toString()) return df.format(this) } /** * 保留小数,默认四舍五入 */ fun Double.keepDecimalPlaces(...
Most of time, the headerFormat、pointFormat、footerFormat HTML string is enough for customizing chart tooltip string content, However, sometimes the needs of APP is so weird to satified, in this time, you can even customize the chart tooltip style through JavaScript function. For example, ...
val format = Json { explicitNulls = false } @Serializable data class Project( val name: String, val language: String, val version: String? = "1.2.2", val website: String?, val description: String? = null ) fun main() { val data = Project("kotlinx.serialization", "Kotlin", null,...