Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

Int main()

{

int Value = 242;

cout << "Value = " << Value << endl;

cout << "Value = " << hex << Value << endl;

Console::WriteLine();

return 0;

}

This would produce:

Value = 242

Value = f2

If you have a hexadecimal integer and would like to display it in a decimal format, simply use the cout class followed by the value. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

Int main()

{

cout << "Number = " << 0xFE28D;

Console::WriteLine();

return 0;

}

This would produce:

Number = 1041037

Although all integers are displayed in decimal format by default, you can explicitly ask the compiler to reinforce that notation. You can also display the same value in either format as follows:

#include <iostream>

using namespace std;

using namespace System;

int main()

{

int Variable = 28045;

cout << "Declared Variable: " << Variable << endl;

cout << "In decimal: " << dec << Variable << endl;

cout << "In hexadecimal: " << hex << Variable << endl;

Console::WriteLine();

return 0;

}

This would produce:

Declared Variable: 28045

In decimal: 28045

In hexadecimal: 6d8d

 

8.2.4The uppercase Attribute

As you have found out, the hex attribute is used to display the hexadecimal equivalent of a decimal number, but such a number displays in lowercase. To display the hexadecimal letters to uppercase, include the uppercase attribute. Here is an example:

#include <iostream>

using namespace std;

using namespace System;

int main()

{

int Value = 242642;

cout << "Value = " << Value << endl;

cout << "Value = " << hex << uppercase << Value << endl;

Console::WriteLine();

return 0;

}

This would produce:

Value = 242642

Value = 3B3D2

The uppercase attribute can precede or succeed the hex attribute.

8.2.5Setting the I/o Flag

The width() is used to right-align the value(s) displayed that otherwise would be left aligned. If you want to explicitly left align a value, you can use setiosflags(). To use setiosflags(), you must qualify its flags using the library in which the flag is defined. setiosflags is defined in the iomanip.h header file of the std namespace.

The ios::left flag is used to left-align a value. Here is a simple example that displays an integer:

#include <iostream>

#include <iomanip>

using namespace std;

using namespace System;

Int main()

{

double PropertyValue = 288015;

Console::Write(L"Property Value: ");

cout << setiosflags(ios::left) << PropertyValue;

return 0;

}

This would produce:

Property Value: 288015

After using setiosflags(), any value displayed would follow its rules. To remove the formatting set by setiosflags(), use resetiosflags(), usually with the same argument. Here is an example:

#include <iostream>

#include <iomanip>

using namespace std;

using namespace System;

Int main()

{

double PropertyValue = 288015;

int YearBuilt = 1966;

Console::Write(L"Property Value: ");

cout << setiosflags(ios::left) << PropertyValue;

Console::WriteLine();

Console::Write(L"Year Buil: ");

cout << resetiosflags(ios::left) << setw(8) << YearBuilt;

return 0;

}

This would produce:

Property Value: 288015

Year Buil: 1966

8.2.6Formatting Floating-Point Numbers in C++

In most programs that use precise numbers, you need to be able to format your values to display appropriately. This is made possible with the use of setprecision(). This takes one value in its parentheses as the number of decimal places displayed after the period of the floating number. In order to effectively use setprecision(), first call setiosflags() with the appropriate flag. The ios::fixed flag allows you to specify the exact number of decimal values used in setprecision(). Here is an example:

#include <iostream>

#include <iomanip>

using namespace std;

using namespace System;