If you want everything before the dot there is substringBefore():

val temp = main.getString("temp").substringBefore(".") + "°C"
Answer from forpas on Stack Overflow
Discussions

android - How do you remove extra zeros at the end of a decimal in Kotlin? - Stack Overflow
I'm creating a function that rounds large numbers over 1,000 and then returns a string of that rounded number. For example, "2374293" would return as "2.37m" However, I dont wan... More on stackoverflow.com
🌐 stackoverflow.com
Kotlin calculator can't remove double .0 in the end - Stack Overflow
Using Doubles for this sort of ... without having to do dodgy rounding to hide inaccuracies. And you can set their scale to give as many or as few decimal places as you need.... More on stackoverflow.com
🌐 stackoverflow.com
How do you round a number to N decimal places - Support - Kotlin Discussions
So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
2
August 2, 2018
Problem with rounding a number.
To my eyes, it is fundamentally wrong to try to "round an FP number to two decimals". It should only be a way of formatting a string that describes it. Most two-decimal numbers aren't exactly representable in FP so you shouldn't do any calculations after "rounding". That's why, as a matter of coding practice, the advice to use string formatting functions is correct. More on reddit.com
🌐 r/Kotlin
11
4
August 8, 2018
🌐
GitHub
gist.github.com › jongha › e7acfb24ef9cbfb0c4c41887b50b03bd
Remove trailing zeros in Kotlin · GitHub
Remove trailing zeros in Kotlin. GitHub Gist: instantly share code, notes, and snippets.
Top answer
1 of 2
3

Once you have converted the number to a string with up to 2 decimal places (as you are doing), you can use dropLastWhile to drop trailing zeros and decimal places.

Here is an example

fun prettyFormat(input: Double): String {
    if( input == 0.0 ) return "0"
    
    val prefix = if( input < 0 ) "-" else ""
    val num = abs(input)

    // figure out what group of suffixes we are in and scale the number
    val pow = floor(log10(num)/3).roundToInt()
    val base = num / 10.0.pow(pow * 3)

    // Using consistent rounding behavior, always rounding down since you want
    // 999999999 to show as 999.99M and not 1B
    val roundedDown = floor(base*100)/100.0

    // Convert the number to a string with up to 2 decimal places
    var baseStr = BigDecimal(roundedDown).setScale(2, RoundingMode.HALF_EVEN).toString()

    // Drop trailing zeros, then drop any trailing '.' if present
    baseStr = baseStr.dropLastWhile { it == '0' }.dropLastWhile { it == '.' }

    val suffixes = listOf("","k","M","B","T")

    return when {
        pow < suffixes.size -> "$prefix$baseStr${suffixes[pow]}"
        else -> "${prefix}infty"
    }
}

This produces

11411.0   = 11.41k
11000.0   = 11k
9.99996E8 = 999.99M
12.4      = 12.4
0.0       = 0
-11400.0  = -11.4k

If you don't care about zero or negative numbers it can be simplified a bit.

2 of 2
3
import java.math.BigDecimal
import java.math.RoundingMode.HALF_EVEN

fun roundBigNumber(number: Long): String {
  fun calc(divisor: Long) = BigDecimal(number.toDouble() / divisor)
    .setScale(2, HALF_EVEN)
    .toString()
    .dropLastWhile { it == '0' }
    .dropLastWhile { it == '.' }
  return when {
    number in 1000..999994                     -> calc(1000) + "k"
    number in 999995..999999                   -> "999.99k"
    number in 1000000..999994999               -> calc(1000000) + "m"
    number in 999995000..999999999             -> "999.99m"
    number in 1000000000..999994999999         -> calc(1000000000) + "b"
    number in 999995000000..999999999999       -> "999.99b"
    number in 1000000000000..999994999999999   -> calc(1000000000000) + "t"
    number in 999995000000000..999999999999999 -> "999.99t"
    number >= 1000000000000000                 -> "∞"
    else                                       -> number.toString()
  }
}
🌐
Android Developers
developer.android.com › api reference › decimalformat
DecimalFormat | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português Brasil · Tiếng Việt · 中文 – 简体
🌐
Baeldung
baeldung.com › home › kotlin › kotlin numbers › convert double to string removing scientific notation
Convert Double to String Removing Scientific Notation | Baeldung on Kotlin
July 6, 2024 - In this tutorial, we’ll explore different techniques in Kotlin to convert double values to string representations without resorting to scientific notation. Scientific notation is the standard format for representing floating-point numbers using an exponent denoted by E or e. This notation expresses numbers as a base raised to a power of 10. For example, we can represent the number 12345 as 1.2345E4. Similarly, the decimal number 0.012345 can be expressed in scientific notation as 1.23450E-02 using the negative exponent of 10.
Find elsewhere
🌐
Kotlin
kotlinlang.org › api › latest › jvm › stdlib › kotlin › to-big-decimal.html
toBigDecimal
December 7, 2021 - This annotation marks the standard ... may be removed completely in any further release. ... This annotation marks the experimental preview of the language feature SubclassOptInRequired. ... Marks the API that is dependent on the experimental unsigned types, including those types themselves. ... In previous versions of Kotlin, the generated ...
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - Support - Kotlin Discussions
August 2, 2018 - So I saw this post about my problem(Print floats with certain amount of decimal numbers) And I was wondering I it possible to use a methood or something else rather “%.2f”.format(value) in order to achive the same thing …
🌐
Baeldung
baeldung.com › home › java › java numbers › convert double to string, removing decimal places
Convert Double to String, Removing Decimal Places | Baeldung
January 8, 2024 - In this tutorial, we’ll take a look at the different ways to convert a double value to a String, removing its decimal places.
🌐
Reddit
reddit.com › r/kotlin › double number showing different values after decimal point in different views
r/Kotlin on Reddit: Double number showing different values after decimal point in different views
July 4, 2024 - I use the same String.format("%.1f", ...) method to display in the logcat/TextView as well, as I only want single digit after decimal.
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How do you round a number to N decimal places - #8 by dalewking - Support - Kotlin Discussions
September 19, 2019 - You need to realize that there is a difference between rounding for output, which you can do with String.format and other methods and having a value that exactly equals 3.14. If you use the double type you will NEVER, EVER have a value that exactly equals 3.14.
🌐
Reddit
reddit.com › r/kotlin › converting double to string with specific number of decimals
r/Kotlin on Reddit: Converting Double to String with specific number of decimals
May 29, 2021 -

Hello, I'm doing kata on codewars and I'm stuck on converting my results to String. The test shows

 expected:<[10, 3.0418396189[]]> but was:<[10, 3.0418396189[294032]]> 

so, while calculations work fine, it seems I need to cut my answer to 10 decimals before converting it to String. I've tried to use toBigDecimal() but can't understand how to implement it. Documentation says it should be like this

fun Double.toBigDecimal(mathContext: MathContext): BigDecimal

but when I write it as

result.toBigDecimal(MathContext(11, RoundingMode.HALF_UP)).toString()

I get an error 'unresolved reference: MathContext '

Is the way I write this code wrong or I need to import something for this to work, mb there is just better way to do it, any help is appreciated.

🌐
Kotlin Discussions
discuss.kotlinlang.org › support
Format a double to fixed decimal length - Support - Kotlin Discussions
November 28, 2020 - Hi I want to round a Double (12345.12345) to a String with a fixed length after the ‘.’ Example: 12345.123 And: if it is 12345.1 It still should give me 12345.100 How can I do this in kotlin?
🌐
Luasoftware
code.luasoftware.com › tutorials › kotlin › kotlin-round-double-to-2-decimal-point
Kotlin Round Double to 2 Decimal Point - Lua Software Code
val value = 3.14159265358979323// round to 2 decimal: 3.14"%.2f".format(value).toDouble()// orMath.round(value * 100) / 100.0
🌐
MojoAuth
mojoauth.com › binary-encoding-decoding › decimal-with-kotlin
Decimal with Kotlin | Binary Encoding Techniques Across Programming Languages
Working with financial data in Kotlin often means grappling with floating-point inaccuracies. This guide dives into using java.math.BigDecimal to handle decimal arithmetic precisely, avoiding the pitfalls of Float and Double.
🌐
Javaer101
javaer101.com › en › article › 36272551.html
How can I remove the decimal parts from numbers in Kotlin? - Javaer101
Collected from the Internet · Please contact [email protected] to delete if infringement