Scala String Programs
- How to determine if a string contains a regular expression in Scala?
- Scala program to split string
- Scala program to convert string to integer
- How to count the number of characters in a string in Scala?
- How to create a range of characters in Scala?
- How to left-trim and right-trim strings in Scala?
- How to get date, month and year as string or number in Scala?
- How to replace a regular expression pattern in a string in Scala?
- Extracting difference, intersection, and distinct characters between two strings in Scala
- How to convert a string to byte array in Scala?
- How to convert byte array to string in Scala?
- How to convert a string to date in Scala?
- How to convert hex string to int in Scala?
- How to convert hex string to long in Scala?
- How to convert Int to Double in Scala?
- Scala program to compare strings using equal to (==) operator
- Scala program to compare strings using equals() method
- Scala program to compare strings using compareTo() method
- Scala program to compare strings using equalsIgnoreCase() method
- Scala program to concatenate strings using the plus (+) operator
- Scala program to concatenate strings using the concat() method
- Scala program to extract the substring from a string
- Scala program to find the length of the string
- Scala program to get characters from the string
- Scala program to check the string starts with a specified substring
- Scala program to check the string ends with specified substring
- Scala program to get bytes array from string using getBytes() method
- Scala program to copy the content of string into a character array
- Scala program to copy the content of string into a character array
- Scala program to convert the string into a character array
- Scala program to convert an integer number into a string
- Scala program to convert a floating-point number into a string
- Scala program to get the hash code of the string
- Scala program to get the index of the first occurrence of the specified substring
- Scala program to get the index of the last occurrence of the specified substring
- Scala program to replace the character with another character in the string
- Scala program to demonstrate the replaceAll() method
- Scala program to demonstrate the replaceFirst() method
- Scala program to split the string based on the given separator
- Scala program to demonstrate the subSequence() method
- Scala program to convert the string into lowercase
- Scala program to convert the string into uppercase
- Scala program to trim a string
- Scala program to concatenate string using \'s\' method of string interpolation
- Scala program to concatenate formatted string using \'f\' method of string interpolation
- Scala program to print raw string using the raw method of string interpolation
- Scala program to create a string using StringBuilder class
- Scala program to append a character to an immutable string using the \'+=\' operator
- Scala program to append a string to an immutable string using the \'++=\' operator
- Scala program to append a number to an immutable string using append() method
- Scala program to clear the content of StringBuilder string
- Scala program to delete a substring from StringBuilder string
- Scala program to insert a substring into StringBuilder string
- Scala program to replace a substring into StringBuilder string
- Scala program to get the length of StringBuilder string
- Scala program to get characters from StringBuilder string
- Scala program to check whether a string is empty or not
- Scala program to check nullable string
- Scala program to create a string using StringBuffer class
- Scala program to demonstrate the append() method with StringBuffer class
- Scala program to delete a substring from a string created using StringBuffer
- Scala program to insert a substring into string created using StringBuffer
Method 1: Converting Int to Double in Scala using toDouble method
The toDouble method in Scala is used to convert data type form any numeric type to Double in Scala.
Syntax:
Program to convert Int to Double
Output:
Explanation:
In the above code, we have created a variable named intVar of Int type to store integer value. Then we have converted it to a Double value using the toDouble method and store it into variable doubleVar. Then at last we have printed the value using the println method.
Method 2: Converting Int to Double in Scala using asInstanceOf method
The asInstanceOf[] method is used for converting between two data types. You can convert any data type to others using the asInstanceOf[] method.
This method is derived from java.lang package, though it does not an import statement, but it will work on for java data types i.e. not all Scala types can be converted using this method.
Syntax:
Program to convert int to double in Scala
Output:
Explanation:
In the above code, we have created a variable named intVar of Int type to store an integer value. Then we have converted it to a Double value using the asInstanceOf[Double] method and store it into variable doubleVar. Then at last we have printed the value using the println method.
need an explanation for this answer? contact us directly to get an explanation for this answer