Here, we will read a numeric string from the user using the readLine() function and then convert the string to a float number using the Float() function and print the number on the console screen.
The source code to read a float number from the user is given below. The given program is compiled and executed successfully.
// Swift program to read a float number
// from the user
import Swift;
print("Enter Number:")
if let val = readLine(){
if let num = Float(val)
{
print("Entered number is: ",num);
}
}
Output:
Enter Number:
3.14
Entered number is: 3.14
...Program finished with exit code 0
Press ENTER to exit console.
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
import Swift;
Here, we read a numeric string using the readLine() function. Then convert the string into a float number using the Float() function and print the result on the console screen.
Program/Source Code:
The source code to read a float number from the user is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we imported a package Swift to use the print() function using the below statement,
Here, we read a numeric string using the readLine() function. Then convert the string into a float number using the Float() function and print the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer