DEV Community

Cover image for C# da Formatlash
Sabrina
Sabrina

Posted on

2 2 2 2 3

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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (1)

Collapse
 
mushtariy profile image
Mushtariy

vaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaayyyyyyyyyyyyyyyyy

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more