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

8.6Formatting Data Display

8.6.1 Introduction

Instead of using two Write() or a combination of Write() and WriteLine() to display data, you can convert a value to a string and display it. To do this, you can provide two strings to Write() or WriteLine() and separate them with a comma:

  1. The first part of the string provided to Write() or WriteLine() is the complete string that will display to the user. This first string itself can be made of different sections:

    1. One section is a string in any way you want it to display

    2. Another section is a number included between an opening curly bracket "{" and a closing curly bracket "}". This combination of "{" and "}" is referred to as a placeholder

You can put the placeholder anywhere inside of the string. The first placeholder must have number 0. The second must have number 1, etc. With this technique, you can create the string anyway you like and use the placeholders anywhere inside of the string

  1. The second part of the string provided to Write() or WriteLine() is the value that you want to display. It can be one value if you used only one placeholder with 0 in the first string. If you used different placeholders, you can then provide a different value for each one of them in this second part, separating the values with a comma

Here are examples:

using namespace System;

Int main()

{

String ^ FullName = L"Anselme Bogos";

int Age = 15;

double HSalary = 22.74;

Console::WriteLine(L"Full Name: {0}", FullName);

Console::WriteLine(L"Age: {0}", Age);

Console::WriteLine(L"Distance: {0}", HSalary);

return 0;

}

This would produce:

Full Name: Anselme Bogos

Age: 15

Distance: 22.74

As mentioned already, the numeric value typed in the curly brackets of the first part is an ordered number. If you want to display more than one value, provide each incremental value in its curly brackets. The syntax used is:

Write("To Display {0} {1} {2}", First, Second, Third);

You can use the sections between a closing curly bracket and an opening curly bracket to create a meaningful sentence.

8.6.2Conversion To String

We mentioned earlier that everything the user types using the keyboard is primarily a string and it's your job to convert it to the appropriate type. In reverse, if you have a value that is not a string, you can easily convert it to a string. To support this, each .NET Framework data type provides a mechanism called ToString.

To conversion a value of a primitive data type to a string, type the name of the variable, followed by a period, followed by ToString(). Here is an example:

using namespace System;

Int main()

{

String ^ FullName = L"Anselme Bogos";

int Age = 15;

double HSalary = 22.74;

Console::WriteLine(L"Full Name: {0}", FullName);

Console::WriteLine(L"Age: {0}", Age.ToString());

Console::WriteLine(L"Distance: {0}", HSalary.ToString());

return 0;

}

In some cases, you will type something in the parentheses of ToString().

8.6.3Number Formatting

To properly display data in a friendly and most familiar way, you can format it. Formatting tells the compiler what kind of data you are using and how you want the compiler to display it to the user. You can display a natural number in a common value or, depending on the circumstance, you may prefer to show it as a hexadecimal value. When it comes to double-precision numbers, you may want to display a distance with three values on the right side of the decimal separator and in some cases, you may want to display a salary with only 2 decimal places.

The System namespace provides a specific letter that you can use in the Write() or WriteLine()'s placeholder for each category of data to display. To format a value, in the placeholder of the variable or value, after the number, type a colon and one of the appropriate letters from the following table. If you are using ToString(), then, in the parentheses of ToString(), you can include a specific letter or combination inside of double-quotes. The letters and their meanings are:

 

Character

Used For

 

c

C

Currency values

 

d

D

Decimal numbers

 

e

E

Scientific numeric display such as 1.45e5

 

f

F

Fixed decimal numbers

 

g

G

General and most common type of numbers

 

n

N

Natural numbers

 

r

R

Roundtrip formatting

 

x

X

Hexadecimal formatting

 

p

P

Percentages

Here are examples:

using namespace System;