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!😊

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more