DEV Community

Cover image for Formatlash
umida5
umida5

Posted on

1 1 1 1 1

Formatlash

Bugun Formatlash metodlarini o'rganamiz

C# dasturlash tilida formatlash turli usularda amalga oshiriladi.Eng ko'p ishlatilaigan biri String.Format va Interpolation ishlatiladi.

1.InterPolation metodi - $ belgisi orqali formatlab chop etish,qo'shtirnoq ichida injuct(ukol) qilish,o'zgaruvchidan fo'ydalanish.

for example:

` Console.Write("enter the number: ");
 var number = Convert.ToInt32(Console.ReadLine());
 Console.Write("Enter your name: ");
 string ism = Console.ReadLine();

 Console.WriteLine($"Mening ismim : {ism}. \nMen kiritgan raqam: {number}");
`
Enter fullscreen mode Exit fullscreen mode

2.Formatlash yordamida sonlarni ko'rinishini o'zgartirish
Bu misolda {0:C} format kodi 'C' (Currency) belgisi yordamida qiymatni valyuta ko'rinishida chiqaradi.
Example:

 double narx = 1234.56;
 string formatlanganNarx = String.Format("Narx: {0:C}", narx);
 Console.WriteLine(formatlanganNarx);
Enter fullscreen mode Exit fullscreen mode

3.Sonlarni xonalar bo'yicha fo'rmatlash
Bu misolda **{0:D5}* format kodi D (Decimal) va 5 son necha xonada chiqishini ko'rsatadi.

example:

 int son = 42;
 string formatlanganSon = String.Format("Son: {0:D5}", son);
 Console.WriteLine(formatlanganSon);
Enter fullscreen mode Exit fullscreen mode

3.Foiz Farmatlash
Agar siz qiymatni foiz sifatida ko'rsatmoqchi bo'lsangiz {0:P} format kodidan foydalanishingiz mumkin.

double foiz = 0.857;
Enter fullscreen mode Exit fullscreen mode

Bu misolda {0:P} format kodi qiymatni foiz sifatida chiqaradi va kasr qismi uchun ikki xonnani saqlab qoladi.

string formatlanganFoiz = String.Format("Foiz: {0:P2}", foiz);
Console.WriteLine(formatlanganFoiz);
Enter fullscreen mode Exit fullscreen mode

4.Satr uzunligini belgilash
Agar qiymatni ma'lum uzunlikka keltirish kerak bo'lsa, siz xonalar sonini belgilashingiz mumkin.

int son = 42;
string formatlanganSon = String.Format("Son: {0, 7}",son);
Console.WriteLine(formatlanganSon);
Enter fullscreen mode Exit fullscreen mode

E'tiboringiz uchun raxmat!😊

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay