C# da Formatlash

sabrina_abcdna123

Sabrina

Posted on August 14, 2024

C# da Formatlash

Formatlash

C# dasturlash tilida formatlash turli usullarda amalga oshiriladi.Eng ko'p ishlatiladigan usullardan biri String.Format ** va **Interpolation ($ belgisi yordamida) ishlatiladi.

String.Formatga misol:

int yosh = 24;
double KasrSon = 12.2;
string ism = "ilmhub";
string natija  = Strong.Format("Mening yoshim {0}, \nMen kiritgan kasrSon : {1}, \nMening ismim {2}",yosh,KasrSon,ism

Console.WriteLine(natija);
Enter fullscreen mode Exit fullscreen mode

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

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'zgratirish.
Bu misolda {0:C} format kodi 'C'(Currency) belgisi yordamida qiymatni valyuta ko'rinishida chiqaradi.
Misol:

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

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

int son = 42;
string formatlanganSon = StringFormat("Son:{0:D5},son);
Console.WriteLine(formatlanganSon);

class Dastur 
{
static void Main()
{
double narx = 1234.56;
strig natija = narx.ToSrting("C");
Console.WriteLine($"Narx:{natija}");
}
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
sabrina_abcdna123
Sabrina

Posted on August 14, 2024

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

Sign up to receive the latest update from our blog.

Related

C# da Formatlash
csharp C# da Formatlash

August 14, 2024