Elementary Programming in C++: Numeric Type Conversions

Floating-point numbers can be converted into integers using explicit casting.

Can you assign an integer value to a floating-point variable? Yes. Can you assign a floating­point value to an integer variable? Yes. When assigning a floating-point value to an integer variable, the fractional part of the floating-point value is truncated (not rounded). For example:

int i = 34.7;     // i becomes 34
double f = i;     // f is now 34
double g = 34.3// g becomes 34.3
int j = g;        // j is now 34

Can you perform binary operations with two operands of different types? Yes. If an integer and a floating-point number are involved in a binary operation, C++ automatically converts the integer to a floating-point value. So, 3 * 4.5 is the same as 3.0 * 4.5.

C++ also allows you to convert a value from one type to another explicitly by using a casting operator  casting operator. The syntax is

static_cast<type>(value)

where value is a variable, a literal, or an expression and type is the type you wish to convert the value to.

For example, the following statement

cout << static_cast<int>(1.7);

displays 1. When a double value is cast into an int value, the fractional part is truncated.

The following statement

cout << static_cast<double>(1) / 2;

displays 0.5, because 1 is cast to 1.0 first, then 1.0 is divided by 2. However, the statement

cout << 1 / 2;

displays 0, because 1 and 2 are both integers and the resulting value should also be an integer.

Note

Static casting can also be done using the (type) syntax—that is, giving the target type in parentheses, followed by a variable, a literal, or an expression. This is called the C-style cast. For example,

int i = (int)5.4;

This is the same as

int i = static_cast<int>(5.4);

The C++ static_cast operator is recommended by the ISO standard. It is preferable to the C-style cast.

Casting a variable of a type with a small range to a variable of a type with a larger range is known as widening a type. Casting a variable of a type with a large range to a variable of a type with a smaller range is known as narrowing a type. Narrowing a type, such as assigning a double value to an int variable, may cause loss of precision. Lost information might lead to inaccurate results. The compiler gives a warning when you narrow a type, unless you use static_cast to make the conversion explicit.

Listing 2.10 gives a program that displays the sales tax with two digits after the decimal point.

Listing 2.10 SalesTax.cpp

1 #include <iostream>

2 using namespace std;

3

4 int main()

5 {

6    // Enter purchase amount

7    double purchaseAmount;

8    cout << “Enter purchase amount: “;

9    cin >> purchaseAmount;

10

11    double tax = purchaseAmount * 0.06;

12    cout << “Sales tax is ” << static_cast<int>(tax * 100) / 100.0;

13

14    return 0;

15 }

Variable purchaseAmount stores the purchase amount entered by the user (lines 7-9). Suppose the user entered 197.55. The sales tax is 6% of the purchase, so the tax is evaluated as 11.853 (line 11). The statement in line 12 displays the tax 11.85 with two digits after the decimal point. Note that

tax * 100 is 1185.3

static_cast<int>(tax * 100) is 1185

static_cast<int> (tax * 100) / 100.0 is 11.85

So, the statement in line 12 displays the tax 11.85 with two digits after the decimal point.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *