C# 0 The Complete Reference


#.## , which tells  WriteLine( )



Download 4,07 Mb.
Pdf ko'rish
bet99/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   95   96   97   98   99   100   101   102   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

#.##

, which tells 



WriteLine( )

 to display two decimal places. 

It is important to understand, however, that 

WriteLine( )

 will display more than one digit 

to the left of the decimal point, if necessary, so as not to misrepresent the value.

Here is another example. This statement

Console.WriteLine("{0:###,###.##}", 123456.56);

generates this output:

123,456.56

If you want to display monetary values, use the 



C

 format specifier. For example:

decimal balance;

balance = 12323.09m;

Console.WriteLine("Current balance is {0:C}", balance);

The output from this sequence is shown here (in U.S. dollar format):

Current balance is $12,323.09

The


C

 format can be used to improve the output from the price discount program 

shown earlier:

// Use the C format specifier to output dollars and cents.

using System;

www.freepdf-books.com




46

 

P a r t   I :  



T h e   C #   L a n g u a g e

class UseDecimal {

  static void Main() {

    decimal price;

    decimal discount;

    decimal discounted_price;

    // Compute discounted price.

    price = 19.95m;

    discount = 0.15m; // discount rate is 15%

    discounted_price = price - ( price * discount);

    Console.WriteLine("Discounted price: {0:C}", discounted_price);

  }


}

Here is the way the output now looks:

Discounted price: $16.96

Literals

In C#, 


literals

 refer to fixed values that are represented in their human-readable form. 

For example, the number 100 is a literal. For the most part, literals and their usage are 

so intuitive that they have been used in one form or another by all the preceding sample 

programs. Now the time has come to explain them formally.

C# literals can be of any simple type. The way each literal is represented depends upon 

its type. As explained earlier, character literals are enclosed between single quotes. For 

example, ‘a’ and ‘%’ are both character literals.

Integer literals are specified as numbers without fractional components. For example, 

10 and –100 are integer literals. Floating-point literals require the use of the decimal point 

followed by the number’s fractional component. For example, 11.123 is a floating-point literal. 

C# also allows you to use scientific notation for floating-point numbers.

Since C# is a strongly typed language, literals, too, have a type. Naturally, this raises the 

following question: What is the type of a numeric literal? For example, what is the type of 

12, 123987, or 0.23? Fortunately, C# specifies some easy-to-follow rules that answer these 

questions.

First, for integer literals, the type of the literal is the smallest integer type that will hold 

it, beginning with 




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   95   96   97   98   99   100   101   102   ...   1096




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish