DEV Community

Cover image for C# da Formatlash
Sabrina
Sabrina

Posted on

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

Top comments (1)

Collapse
 
mushtariy profile image
Mushtariy

vaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyyyyyyyyy