Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 423 Vote(s) - 3.52 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Format number using decimal format in kotlin

#1
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
Reply

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

[To see links please register here]

).
Reply

#3
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")



Reply

#4
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:

[To see links please register here]

Reply

#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))
}
Reply

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

```kotlin
"%,.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](

[To see links please register here]

) is one option.
Reply

#7
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)) ?: ""
```
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through