Kotlin Array.toDoubleArray()

Kotlin Array.toDoubleArray() Tutorial

The Array.toDoubleArray() function in Kotlin is used to convert an Array of Double type values, into a DoubleArray. It creates a new DoubleArray containing the elements of the given original array.

This is just a conversion of data from Array<Double> type to DoubleArray type.

This tutorial will explore the syntax of the Array.toDoubleArray() function and provide examples of its usage in Kotlin arrays.

Syntax

The syntax of the Array.toDoubleArray() function is as follows:

fun Array<out Double>.toDoubleArray(): DoubleArray

Examples for Array.toDoubleArray() function

1. Convert Array of double precision numbers to DoubleArray

In this example, we’ll take an array of double precision values in originalArray, and convert this Array into an DoubleArray using Array.toDoubleArray().

Kotlin Program

fun main() {
    val originalArray: Array<Double> = arrayOf(100.00412, 1.236542)

    // Using toDoubleArray() to convert the Array of double precision numbers to a DoubleArray
    val doubleArray = originalArray.toDoubleArray()

    // Printing the original array and the resulting DoubleArray
    println("Given Array:\n${originalArray.contentToString()}\n")
    println("Double Array:\n${doubleArray.contentToString()}")
}

Output

Given Array:
[100.00412, 1.236542]

Double Array:
[100.00412, 1.236542]

Summary

In this tutorial, we’ve covered the Array.toDoubleArray() function in Kotlin, its syntax, and how to use it to convert an Array of Double values into an DoubleArray.