C# - Formatlash.

sunnat_qayumov

Sunnat Qayumov

Posted on August 15, 2024

C# - Formatlash.

C# dasturlash tilida formatlash turli uslublarda amalga oshiriladi. Eng ko'p ishlatiladigan uslublardan biri bu String.Format va Interpolation($).

String.Formatga misol:

class Programm
{ 
    public static void Main()
    {
        int yosh = 24;
        double kasrSon = 12.2;
        string ism = "ilmhub";
        string natija = String.Format("Mening yoshim {0},\nMen kiritgan kasrSon: {1}, \n Mening ismim {2}", yosh, kasrSon, ism};
        Console.WriteLine(natija);
    }
}
Enter fullscreen mode Exit fullscreen mode

Interpolation - $ belgisi orqali formatlab chop etish, qo'shtirnoq ichida inject(ukol) qilish, o'zgaruvchilardan foydalanish.
Interpolationga misol:

class Programm
{
    public static void Main()
    {
        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

Formatlash yordamida sonlarni ko'rinishini o'zgartirish.
Misol1:

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

Bu misolda {0:C} format kodi 'C' (Currentcy) belgisi yordamida qiymatni valyuta ko'rinishida chiqarib beradi.

Misol2:

class Dastur
{
    public static void Main()
    {
        double narx = 1234.56;
        string formatlanganNarx = narx.ToString("C");
        Console.WriteLine($"Narx: ");
    }
}
Enter fullscreen mode Exit fullscreen mode

Sonlarni xonalar bo'yicha formatlash:

class Programm
{
    public static void Main()
    {
        int son = 45;
        string formatlanganSon = String.Format("Son: {0:D5}", son);
        Console.WriteLine(formatlanganSon);
    }
}
Enter fullscreen mode Exit fullscreen mode

Bu misolda {0:D5} format kodi 'D' (Decimal) va 5 son necha xona chiqarishini ko'rsatadi.

float yoki double sonlarni formatlash:

class Programm
{
    public static void Main()
    {
        double son = 12.34567;
        string formatlanganSon = String.Format("Son: {0:F2}", son);
        Console.WriteLine(formatlanganSon);
    }
}
Enter fullscreen mode Exit fullscreen mode

Bu misolda {0:F2} format kodi sonni faqat ikkita kasr xonasigacha(2 decimal places) chiqarib beradi.

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

class Programm
{
    public static void Main()
    {
       double foiz = 0.857;
       string formatlanganFoiz = String.Format("Foiz: {0:P2}", foiz);
       Console.WriteLine(formatlanganFoiz);
    }
}
Enter fullscreen mode Exit fullscreen mode

Bu misolda {0:P2} format kodi qiymatni foiz sifatida chiqaradi va kasr qismi uchun ikki xonani saqlab beradi.

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

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

Bu misolda {0: 7} format kodi qiymatni besh xonali qilib chiqaradi, oldini bosh joylar(space) bilan to'ldiradi.

💖 💪 🙅 🚩
sunnat_qayumov
Sunnat Qayumov

Posted on August 15, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

C# - Formatlash.
csharp C# - Formatlash.

August 15, 2024

C# - Input.
csharp C# - Input.

August 9, 2024