Home

Decimal Numbers

 

Floating-Point Variables

The integers we have used so far don't allow decimal values. C++ provides floating identifier values that would solve this problem. To declare a variable that involves a decimal value, you can use the float keyword. Here is an example:

using namespace System;

int main()
{
	float Fraction = 12.35;

	Console::WriteLine(Fraction);

	Console::WriteLine();
	return 0;
}

A float variable can hold a number that ranges from 3.4 x 10-38 to 3.4 x 1038. Besides, or instead of, float, you can use the Single data type to declare a variable that holds a simple decimal value. The Single data type is defined in the System namespace.

 

Practical LearningPractical Learning: Using a Single-Precision Variables

  1. To declare a single-precision variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        long propertyNumber = 490724;
        __wchar_t propertyType = 'S';
        Byte stories = 2;
        unsigned bedrooms = 5;
        float bathrooms = 3.5;
        unsigned yearBuilt = 1962;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Property #:    ");
        Console::WriteLine(propertyNumber);
        Console::Write("Property Type:  ");
        Console::WriteLine(propertyType);
        Console::Write("Stories:        ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:       ");
        Console::WriteLine(bedrooms);
        Console::Write("Bathrooms:      ");
        Console::WriteLine(bathrooms);
        Console::Write("Year Built:     ");
        Console::WriteLine(yearBuilt);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property #:    490724
    Property Type:  S
    Stories:        2
    Bedrooms:       5
    Bathrooms:      3.5
    Year Built:     1962
    Press any key to continue . . .
  3. Close the DOS window

Double-Precision Variables

One of the limitations of the float data type is that it provides less precision than required sometimes (in fact, the Microsoft C++ compiler, always displays a warning when you use the float data type to declare a variable). The alternative is to use the double keyword or the Double data type to declare a variable that would hold decimal numbers. The Double data type is defined in the System namespace.

A double-precision variable declared with double or Double can hold a decimal or fractional number that ranges from 1.7 x 10-308 to 1.7 x 10308. It is used for numbers that and/or are very large.

using namespace System;

int main()
{
	System::Double Fraction = 12.35;

	Console::WriteLine(Fraction);

	return 0;
}

This would produce:

12.35
Press any key to continue
 

Practical LearningPractical Learning: Using a Single-Precision Variables

  1. To use a double-precision variable, change the file as follows:
     
    using namespace System;
    
    int main()
    {
        long propertyNumber = 490724;
        __wchar_t propertyType = 'S';
        Byte stories = 2;
        unsigned bedrooms = 5;
        float bathrooms = 3.5;
        unsigned yearBuilt = 1962;
        double marketValue = 652540;
    
        Console::WriteLine("=//=  Altair Realty  =//=");
        Console::WriteLine("-=- Properties Inventory -=-");
        Console::Write("Property #:    ");
        Console::WriteLine(propertyNumber);
        Console::Write("Property Type:  ");
        Console::WriteLine(propertyType);
        Console::Write("Stories:        ");
        Console::WriteLine(stories);
        Console::Write("Bedrooms:       ");
        Console::WriteLine(bedrooms);
        Console::Write("Bathrooms:      ");
        Console::WriteLine(bathrooms);
        Console::Write("Year Built:     ");
        Console::WriteLine(yearBuilt);
        Console::Write("Market Value:   ");
        Console::WriteLine(marketValue);
        
        return 0;
    }
  2. Execute the project to see the result:
     
    =//=  Altair Realty  =//=
    -=- Properties Inventory -=-
    Property #:    490724
    Property Type:  S
    Stories:        2
    Bedrooms:       5
    Bathrooms:      3.5
    Year Built:     1962
    Market Value:   652540
    Press any key to continue . . .
  3. Close the DOS window

Initializing a Double-Precision Variable

You may have found out that when you declare and initialize a float or Single variable, the compiler generates a warning. Consider the following example:

using namespace System;

int main()
{
	float Fraction = 12.35;

	Console::WriteLine(Fraction);

	Console::WriteLine();
	return 0;
}

When compiled, you would get the following warning:

.\Exercise.cpp(5) : warning C4305: 'initializing' : truncation from 'double' to 'float'

Although the program would compile fine. By default, when the compiler sees a value such as 12.35, for the sake of precision, it tends to store it as a double value. If you really want the variable to be treated as a float or Single, add the f or F to its right. Here is an example:

using namespace System;

int main()
{
	float Fraction = 12.35f;

	Console::WriteLine(Fraction);

	Console::WriteLine();
	return 0;
}

This time, the compiler will not generate a warning. Remember that you can use f (lowercase) or F (uppercase).

 

Previous Copyright © 2006-2016, FunctionX, Inc. Next