# How do you round a number to N decimal places

**URL:** <https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843>\
**Category:** Support\
**Created:** [August 2, 2018, 1:08pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843 "2018-08-02T13:08:19Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![RavenEXP](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@RavenEXP](https://discuss.kotlinlang.org/u/RavenEXP)\
**Post date:** [August 2, 2018, 1:08pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/1 "2018-08-02T13:08:19Z")

</div>

So I saw this post about my problem([Print floats with certain amount of decimal numbers](https://discuss.kotlinlang.org/t/print-floats-with-certain-amount-of-decimal-numbers/1812))

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

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [August 2, 2018, 3:12pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/2 "2018-08-02T15:12:18Z")

</div>

You should explain better you problem.

Technically you cannot round the IEEE 754 floating point `0.1` to the first decimal point.

However you can use

```kotlin
fun Double.round(decimals: Int): Double {
    var multiplier = 1.0
    repeat(decimals) { multiplier *= 10 }
    return round(this * multiplier) / multiplier
}

```

---

<div class="post-metadata">

**Author:** ![RavenEXP](https://avatars.discourse-cdn.com/v4/letter/r/48db29/32.png) [@RavenEXP](https://discuss.kotlinlang.org/u/RavenEXP)\
**Post date:** [August 2, 2018, 4:25pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/3 "2018-08-02T16:25:09Z")

</div>

I’m just trying to short the number to lets say 2 decimal places.

for example I want to turn:  
pi=3.14159265359  
to just  
pi=3.14

I hope you can understand me know, or at least more clear to understand.

---

<div class="post-metadata">

**Author:** ![jaeyeolshin](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jaeyeolshin/32/5966_2.png) [@jaeyeolshin](https://discuss.kotlinlang.org/u/jaeyeolshin)\
**Post date:** [January 16, 2019, 2:28am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/4 "2019-01-16T02:28:00Z")

</div>

If you want to round a double value to specific decimal places and print it, you can use java.math.BigDecimal.

```
import java.math.BigDecimal
import java.math.RoundingMode
val decimal = BigDecimal(3.14159265359).setScale(2, RoundingMode.HALF_EVEN)
println(decimal) // 3.14

```

Or if you just want to compare it with the rounded value, you can use round method.

```
import kotlin.math.round
println(round(3.14159265359 * 100) / 100 == 3.14) // true

```

You can do same thing with @fvasco’s extension method.

```
3.14159265359.round(2) // 3.14

```

---

<div class="post-metadata">

**Author:** ![Drjacky](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@Drjacky](https://discuss.kotlinlang.org/u/Drjacky)\
**Post date:** [September 15, 2019, 7:03pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/5 "2019-09-15T19:03:14Z")

</div>

On `this * multiplier` it shows:

> Required: Int, Found: Double

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [September 16, 2019, 8:01am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/6 "2019-09-16T08:01:17Z")

</div>

Please consider to import `round(Double)` from `kotlin.math.round`

---

<div class="post-metadata">

**Author:** ![Drjacky](https://avatars.discourse-cdn.com/v4/letter/d/43a26b/32.png) [@Drjacky](https://discuss.kotlinlang.org/u/Drjacky)\
**Post date:** [September 16, 2019, 8:14am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/7 "2019-09-16T08:14:20Z")

</div>

`fun Double.round(decimals: Int = 2): Double = "%.${decimals}f".format(this).toDouble()`

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [September 19, 2019, 3:47am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/8 "2019-09-19T03:47:41Z")

</div>

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. Even if you have a statement like:

```
val a = 3.14

```

The variable a does not actually equal the value 3.14 exactly. It actually equals 3.140000000000000124344978758017532527446746826171875 _[Edit: I originally said 3.1400001049041748046875 which is the exact value of the float value 3.14f]_.

If you want a value that exactly equals 3.14 the ONLY way to get it (for the JVM) is with BigDecimal.

---

<div class="post-metadata">

**Author:** ![Jonathan.Haas](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@Jonathan.Haas](https://discuss.kotlinlang.org/u/Jonathan.Haas)\
**Post date:** [September 19, 2019, 8:55am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/9 "2019-09-19T08:55:54Z")

</div>

> [@dalewking](#):
>
> It actually equals 3.1400001049041748046875.

That would be maybe the case with floats (I didn’t checked this), but doubles are much more precise. But of course it won’t be exactly 3.14 either.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [September 19, 2019, 11:34am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/10 "2019-09-19T11:34:11Z")

</div>

It is the same for both float and double that neither can exactly represent 3.14. But I did accidentally show the float representation not the double one. 3.14 as a double is exactly equal to 3.140000000000000124344978758017532527446746826171875.

The only difference between float and double is how close you can get to the number. Float and double can only represent values that are exactly equal to m \* 2 ^ n, where m and n are taken from a subset of the set of integers. The difference between float and double is merely how many bits are allocated for m and n and thus how precise you can be.

Try as hard as you want you will not find any integer values for m and n that give you exactly 3.14.

BigDecimal on the other hand represents numbers of the form m \* 10 ^ n where m and n are integers and are essentially unbounded. m=314 and n=-2 give you the exact value of 3.14.

This is why the best advice when representing money is never use float or double and always use BigDecimal. You can get away with int or long to represent number of pennies as long as you know that the range is limited. See [Java Practices-\>Representing money](http://www.javapractices.com/topic/TopicAction.do?Id=13)

The downside of BigDecimal is that it is slower and more cumbersome to use (less so in Kotlin) as there isn’t built in hardware support for it.

---

<div class="post-metadata">

**Author:** ![fellahi.ali](https://avatars.discourse-cdn.com/v4/letter/f/ce73a5/32.png) [@fellahi.ali](https://discuss.kotlinlang.org/u/fellahi.ali)\
**Post date:** [October 9, 2019, 4:58pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/11 "2019-10-09T16:58:26Z")

</div>

Hi;  
How do you get that exact value of 3.14, I tried to get it as follow:

```auto
val pi = 3.14
println(pi) // I got: 3.14

```

Why?

---

<div class="post-metadata">

**Author:** ![nbilyk](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/nbilyk/32/1483_2.png) [@nbilyk](https://discuss.kotlinlang.org/u/nbilyk)\
**Post date:** [October 9, 2019, 5:10pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/12 "2019-10-09T17:10:05Z")

</div>

For converting numbers to text, I would recommend using java.text.NumberFormat for JVM and Intl.NumberFormat for JS

Those will additionally handle localization for decimal and thousands separators.

---

<div class="post-metadata">

**Author:** ![fatjoe79](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fatjoe79/32/4221_2.png) [@fatjoe79](https://discuss.kotlinlang.org/u/fatjoe79)\
**Post date:** [October 9, 2019, 6:36pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/13 "2019-10-09T18:36:54Z")

</div>

> [@fellahi.ali](#):
>
> I tried to get it as follow:
> 
> ```auto
> val pi = 3.14
> println(pi) // I got: 3.14
> 
> ```
> 
> Why?

The string representation of floating point values in many languages is truncated like that. Even if the value is not exactly 3.14, the output can look like that. This does not mean that it is essentially equal to 3.14. When using the value in computations the exact value is used.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [October 9, 2019, 8:59pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/15 "2019-10-09T20:59:45Z")

</div>

Basically when converting for output Java (which then carries over to Kotlin) will output the shortest string representation that is closer to the exact value than it is to the next representable floating point value. That way if you tried to parse that string representation you will get the same exact value.

The literal 3.14 is converted to a double that has the exact value of 3.140000000000000124344978758017532527446746826171875.

The representable double values on either side of this value are:  
3.13999999999999968025576890795491635799407958984375  
and  
3.1400000000000005684341886080801486968994140625

When converting the value to string it outputs 3.14 because that is the shortest representation that is closest to that exact value than it is to either of these 2 surrounding values.

The fact that it prints 3.14 does not mean that 3.14 is its exact value. Note that this code also prints 3.14:

```
val pi = 3.140000000000000124344978758017532527446746826171875
println(pi)

```

There are in fact an infinite number of text number representations you can put in that code and still get 3.14 out (for example any digits you append to this will not change anything).

FYI, if you want to see what the exact value is, convert it to BigDecimal:

```
println(BigDecimal(pi))

```

---

<div class="post-metadata">

**Author:** ![tagaev](https://avatars.discourse-cdn.com/v4/letter/t/c57346/32.png) [@tagaev](https://discuss.kotlinlang.org/u/tagaev)\
**Post date:** [December 17, 2020, 7:01am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/16 "2020-12-17T07:01:07Z")

</div>

Take a look for this method, in my case it works:

```auto
fun Double.roundTo(numFractionDigits: Int): Double {
  val factor = 10.0.pow(numFractionDigits.toDouble())
  return (this * factor).roundToInt() / factor
}

```

i borrow this from: [java - Round Double to 1 decimal place kotlin: from 0.044999 to 0.1 - Stack Overflow](https://stackoverflow.com/a/51689453/12102080)

---

<div class="post-metadata">

**Author:** ![gidds](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/gidds/32/10865_2.png) [@gidds](https://discuss.kotlinlang.org/u/gidds)\
**Post date:** [December 17, 2020, 8:48am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/17 "2020-12-17T08:48:26Z")

</div>

That may return the nearest Double value to the decimal number you want. But it still won’t be exactly that number (because, except in a few special cases, _no_ Double value is exactly that number).

If you care about exact decimals, don’t use a binary floating-point type like Double; use something like BigDecimal instead.

---

<div class="post-metadata">

**Author:** ![gilmarcosxsantos](https://avatars.discourse-cdn.com/v4/letter/g/3bc359/32.png) [@gilmarcosxsantos](https://discuss.kotlinlang.org/u/gilmarcosxsantos)\
**Post date:** [April 5, 2021, 11:22pm UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/18 "2021-04-05T23:22:38Z")

</div>

Thank you! Helped me a lot.

---

<div class="post-metadata">

**Author:** ![stevehb](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/stevehb/32/8837_2.png) [@stevehb](https://discuss.kotlinlang.org/u/stevehb)\
**Post date:** [November 23, 2021, 8:21am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/19 "2021-11-23T08:21:43Z")

</div>

This is a cursed JavaScript approach. Please just use the one from @fvasco

---

<div class="post-metadata">

**Author:** ![Zeredan](https://avatars.discourse-cdn.com/v4/letter/z/ea666f/32.png) [@Zeredan](https://discuss.kotlinlang.org/u/Zeredan)\
**Post date:** [May 20, 2024, 9:03am UTC](https://discuss.kotlinlang.org/t/how-do-you-round-a-number-to-n-decimal-places/8843/21 "2024-05-20T09:03:19Z")

</div>

also we can write a bit more complex fun to do that:

`infix fun<T : Number> T.roundTo(decimals: Int) : Double{ return pow(10.0, decimals.toDouble()).run{ round(this@roundTo.toDouble() * this) / this } }`
