Given a double value d, typecast it to an integer value and print it.
Examples:
Input: d = 10.23
Output: 10
Explanation: The integer value of 10.23 is 10Input: d = 19.1
Output: 19
Table of Content
Extra Variable
The idea is to convert the given double value into an integer using typecasting and store the result in a separate variable.
Working of Approach:
- Read the double value d.
- Typecast d to an integer using (int)d.
- Store the converted value in a variable result.
- Print result as the output.
#include <iostream>
using namespace std;
int main()
{
double d = 10.23;
// Convert the double value to an integer
int res = (int)d;
// Print the converted integer value
cout << res << endl;
return 0;
}
public class GFG {
public static void main(String[] args)
{
double d = 10.23;
// Convert the double value to an integer
int res = (int)d;
// Print the converted integer value
System.out.println(res);
}
}
d = 10.23
# Convert the double value to an integer
res = int(d)
# Print the converted integer value
print(res)
using System;
class GFG {
static void Main()
{
double d = 10.23;
// Convert the double value to an integer
int res = (int)d;
// Print the converted integer value
Console.WriteLine(res);
}
}
let d = 10.23;
// Convert the double value to an integer
let res = Math.floor(d);
// Print the converted integer value
console.log(res);
Output
10
Typecasting
The idea is to convert the given double value into an integer using typecasting and print the result directly.
Working of Approach:
- Read the given double value d.
- Convert d into an integer using typecasting.
- The fractional part of the value is discarded during conversion.
- Print the resulting integer value directly.
#include <iostream>
using namespace std;
int main()
{
double d = 10.23;
// Convert the double value to an integer and print it
cout << static_cast<int>(d) << endl;
return 0;
}
public class GFG {
public static void main(String[] args)
{
double d = 10.23;
// Convert the double value to an integer and print
// it
System.out.println((int)d);
}
}
d = 10.23
# Convert the double value to an integer and print it
print(int(d))
using System;
class GFG {
static void Main()
{
double d = 10.23;
// Convert the double value to an integer and print
// it
Console.WriteLine((int)d);
}
}
let d = 10.23;
// Convert the double value to an integer and print it
console.log(Math.floor(d));
Output
10