DEV Community

Mushtariy
Mushtariy

Posted on

Formatlash

C# dasturlash tilidaformatlash turli usullarda oshiriladi, eng ko'p ishlatiladigan usullardan biri String.Format va interpolation($ belgisi yordamida) ishlatiladi.

String.Format ga misol;

int yosh = 14;
double kasrSon = 12.2;
string ism = "Mushtariy";
string natija = String.Format{"Mening yoshim {0},/nMen kiritgan kasr {1},/nMening ismim {2}", yosh, kasrSon, ism};
Console.WriteLine(natija);

Enter fullscreen mode Exit fullscreen mode

Formatlash yordamida sonlarni ko'rinishini o'zgartirish mumkin.

Bu misolda {0:C} format kodi 'C' {Currency} yordam beradi;

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

sonlarni xona bo'yicha formatlash

int son = 42;
string ff = String.Format("Son: {0:D5}", son);
Console.WriteLine(ff);
Enter fullscreen mode Exit fullscreen mode

Foiz formatlash
{0:P}formatda yoziladi

double foiz = 0.875;
string son = String.Format("Foiz: {0:P2}", foiz);
Console.WriteLine(son);

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
sabrina_abcdna123 profile image
Sabrina

shik shak shok