在Kotlin中使用十进制格式格式化数字 [英] Format number using decimal format in kotlin

查看:926
本文介绍了在Kotlin中使用十进制格式格式化数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我需要用例如 5000,00 的数字乘以( 1,025 ^ 3 )来进行一些计算.

I am facing an issue where I need to do some calculations with a number like for example 5000,00 multiplied it by (1,025^3).

所以在这种情况下5000,00 * (1,025^3) = 5385,45

所以我的问题是,如何使用小数格式将数字5385,45格式化为 5.385,45 ?

So my question is, how can I format the number 5385,45 to be like 5.385,45 using decimal format maybe?

我自己尝试过,我完成了这段代码,可在应用程序中输出5385,45,但不会输出 5.385,45

I tried by myself and I did this piece of code that outputs 5385,45 in the app but not 5.385,45

    var interestValue = (5000,00*(Math.pow(1.025,yearValue)))
    val number = java.lang.Double.valueOf(interestValue)
    val dec = DecimalFormat("#,00")
    val credits = dec.format(number)
    vValueInterest.text = credits

推荐答案

这是您需要的格式:

val dec = DecimalFormat("#,###.##")

将打印:

5.384,45

如果您始终需要精确到小数点后两位:

if you need always exactly 2 digits after the decimal point:

val dec = DecimalFormat("#,###.00") 

这篇关于在Kotlin中使用十进制格式格式化数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆