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;


This content originally appeared on DEV Community and was authored by Sunnat Qayumov

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);
    }
}

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}");
   }
}

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);
    }
}

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: ");
    }
}

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);
    }
}

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);
    }
}

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);
    }
}

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);
    }
}

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


This content originally appeared on DEV Community and was authored by Sunnat Qayumov


Print Share Comment Cite Upload Translate Updates
APA

Sunnat Qayumov | Sciencx (2024-08-15T16:29:17+00:00) C# – Formatlash.. Retrieved from https://www.scien.cx/2024/08/15/c-formatlash/

MLA
" » C# – Formatlash.." Sunnat Qayumov | Sciencx - Thursday August 15, 2024, https://www.scien.cx/2024/08/15/c-formatlash/
HARVARD
Sunnat Qayumov | Sciencx Thursday August 15, 2024 » C# – Formatlash.., viewed ,<https://www.scien.cx/2024/08/15/c-formatlash/>
VANCOUVER
Sunnat Qayumov | Sciencx - » C# – Formatlash.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/15/c-formatlash/
CHICAGO
" » C# – Formatlash.." Sunnat Qayumov | Sciencx - Accessed . https://www.scien.cx/2024/08/15/c-formatlash/
IEEE
" » C# – Formatlash.." Sunnat Qayumov | Sciencx [Online]. Available: https://www.scien.cx/2024/08/15/c-formatlash/. [Accessed: ]
rf:citation
» C# – Formatlash. | Sunnat Qayumov | Sciencx | https://www.scien.cx/2024/08/15/c-formatlash/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.