Format number using decimal format in kotlin

后端 未结 4 770
渐次进展
渐次进展 2021-01-17 11:46

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

相关标签:
4条回答
  • 2021-01-17 11:57

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

    0 讨论(0)
  • 2021-01-17 12:01

    Used:

    %.numberf
    
    fun main(args: Array<String>) {
    var A: Double
    A = readLine()!!.toDouble()
    var bla = A*A
    var calculator = 3.14159 * bla
    println("A=%.4f".format(calculator))
    }
    
    0 讨论(0)
  • 2021-01-17 12:06
    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

    0 讨论(0)
  • 2021-01-17 12:10

    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") 
    
    0 讨论(0)
提交回复
热议问题