Format number using decimal format in kotlin

匆匆过客 提交于 2020-05-14 18:39:12

问题


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).

So in this case 5000,00 * (1,025^3) = 5385,45

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

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

回答1:


This is the format you need:

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

will print:

5.384,45

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

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



回答2:


val num = 1.34567
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING

println(df.format(num))

When you run the program, the output will be: 1.34

Check: https://www.programiz.com/kotlin-programming/examples/round-number-decimal




回答3:


Try val dec = DecimalFormat("#.###,00"). For examples of DecimalFormat check this link.



来源:https://stackoverflow.com/questions/53848189/format-number-using-decimal-format-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!