Code:
object Question {
//defining the mantissa function which takes a parameter of type double
//and returns a value which is double
def mantissa (number:Double) : Double = {
//Step 1. Convert the double to string and split it at '.'
var splitString:Array[String] = number.toString.split('.')
//Step 2. Concatenate "0." with the decimal part.
//The decimal part is on the second position (index 1) of the splitted string array.
var result:String = "0."+splitString(1)
//return the value after converting it to double
return result.toDouble
}
//main function to call our mantissa function
def main(args: Array[String]) {
//calling mantissa function by passing a parameter and displaying the result
println(mantissa(3.4))
}
}
Screenshot for reference:
Sample Output:
To separate the integer part and decimal part of a double value, we first convert it to the string datatype and use the split() function to separate it by specifying the delimeter as dot ('.'). This returns an array of string. From this array, we can fetch our desired result.
Get Answers For Free
Most questions answered within 1 hours.