45

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
José Nobre
  • 739
  • 2
  • 7
  • 16

6 Answers6

57

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") 
forpas
  • 160,666
  • 10
  • 38
  • 76
  • 11
    0 will be printed as .00 instead of 0.00. Just use `String.format("%.2f", number)` – Choletski Dec 18 '19 at 20:01
  • 2
    @Choletski `0` will be printed as `0`. The question is not about formatting fixed decimal places but about formatting a number like `5385,45` to `5.385,45`, meaning about the thousands separator. – forpas Dec 18 '19 at 20:11
  • 10
    If you need `0` to be printed as `0.00`, you can use `DecimalFormat("#,##0.00")`. – Togashi Sep 16 '20 at 12:05
  • Does `DecimalFormat` work if targeting kotlin-js? – Hakanai Feb 20 '22 at 04:08
  • @Hakanai I only use Kotlin in Android app development so I can't answer your question. – forpas Feb 20 '22 at 10:23
  • After some investigation it turns out it does not, but it turns out `String.format` does not either, so they're about as good as each other. :) Still, you can just use `"%,.2f".format(number)` in this case, you get the thousands separator as well (note the `,` in the format string!) I'm still on the lookout for the portable way to do number formatting without reinventing the wheel. – Hakanai Mar 05 '22 at 04:55
27
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

Karim
  • 962
  • 7
  • 10
21

The "most Kotlin-esque" way I found to do this sort of formatting is:

"%,.2f".format(Locale.GERMAN, 1234.5678)      // => "1.234,57"
"%,.2f".format(Locale.ENGLISH, 1234.5678)     // => "1,234.57"

"%,.2f".format(1234.5678)                     // => "1,234.57" for me, in en_AU

Note though that even though this is Kotlin's own extension method on String, it still only works on the JVM.

For those looking for a multiplatform implementation (as I was), mp_stools is one option.

Hakanai
  • 12,010
  • 10
  • 62
  • 132
5

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))
}
scott_lotus
  • 3,171
  • 22
  • 51
  • 69
3

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

0

I needed to do something similar but for Kotlin Multiplatform (KMM). I struggled to find a multiplatform solution so I thought I'd post the one I came up with here:

// Common
expect fun Double.formatDecimal(maxFractionDigits: Int = 2): String
// Android
import java.text.DecimalFormat

actual fun Double.formatDecimal(maxFractionDigits: Int): String =
    DecimalFormat().apply {
        isGroupingUsed = false
        minimumFractionDigits = 0
        maximumFractionDigits = maxFractionDigits
        isDecimalSeparatorAlwaysShown = false
    }.format(this) 
// iOS
import kotlinx.cinterop.convert
import platform.Foundation.NSNumber
import platform.Foundation.NSNumberFormatter
import platform.Foundation.NSNumberFormatterDecimalStyle

actual fun Double.formatDecimal(maxFractionDigits: Int): String =
    NSNumberFormatter().apply {
        minimumFractionDigits = 0u
        maximumFractionDigits = maxFractionDigits.convert()
        numberStyle = NSNumberFormatterDecimalStyle
    }.stringFromNumber(number = NSNumber(double = this)) ?: ""
Schadenfreude
  • 1,522
  • 1
  • 20
  • 41