---
title: "Density"
description: "A density of the screen. Used for the conversions between pixels, [Dp], [Int] and [TextUnit]."
type: "interface"
---

<div class='type'>Interface</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>



```kotlin
@JvmDefaultWithCompatibility
interface Density : FontScaling
```


A density of the screen. Used for the conversions between pixels, `Dp`, `Int` and `TextUnit`.


## Properties

<div class='sourceset sourceset-common'>Common</div>


```kotlin
val density: Float
```


The logical density of the display. This is a scaling factor for the `Dp` unit.



## Functions

```kotlin
fun Dp.toPx(): Float
```


Convert `Dp` to pixels. Pixels are used to paint to Canvas.


```kotlin
fun Dp.roundToPx(): Int
```


Convert `Dp` to `Int` by rounding


```kotlin
fun TextUnit.toPx(): Float
```


Convert Sp to pixels. Pixels are used to paint to Canvas.


```kotlin
fun TextUnit.roundToPx(): Int
```


Convert Sp to `Int` by rounding


```kotlin
fun Int.toDp(): Dp
```


Convert an `Int` pixel value to `Dp`.


```kotlin
fun Int.toSp(): TextUnit
```


Convert an `Int` pixel value to Sp.


```kotlin
fun Float.toDp(): Dp
```


Convert a `Float` pixel value to a Dp


```kotlin
fun Float.toSp(): TextUnit
```


Convert a `Float` pixel value to a Sp


```kotlin
fun DpRect.toRect(): Rect
```


Convert a `DpRect` to a `Rect`.


```kotlin
fun DpSize.toSize(): Size
```


Convert a `DpSize` to a `Size`.


```kotlin
fun Size.toDpSize(): DpSize
```


Convert a `Size` to a `DpSize`.



## Code Examples

### WithDensitySample
```kotlin
@Composable
@Suppress("UNUSED_VARIABLE")
fun WithDensitySample() {
    val sizeInPx = with(LocalDensity.current) { 16.dp.toPx() }
}
```

