C# etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
C# etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

12 Ağustos 2015 Çarşamba

String.Format ile Formatlı Yazdırma...

String Format for Double [C#]

The following examples show how to format float numbers to string in C#. You can use static method String.Format or instance methods double.ToString and float.ToString.

Digits after decimal point

This example formats double to string with fixed number of decimal places. For two decimal places use pattern „0.00“. If a float number has less decimal places, the rest digits on the right will be zeroes. If it has more decimal places, the number will be rounded.
[C#]
// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

Next example formats double to string with floating number of decimal places. E.g. for maximal two decimal places use pattern „0.##“.
[C#]
// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"

Digits before decimal point

If you want a float number to have any minimal number of digits before decimal point use N-times zero before decimal point. E.g. pattern „00.0“ formats a float number to string with at least two digits before decimal point and one digit after that.
[C#]
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

Thousands separator

To format double to string with use of thousands separator use zero and comma separator before an usual float formatting pattern, e.g. pattern „0,0.0“ formats the number to use thousands separators and to have one decimal place.
[C#]
String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67);       // "12,346"

Zero

Float numbers between zero and one can be formatted in two ways, with or without leading zero before decimal point. To format number without a leading zero use # before point. For example „#.0“ formats number to have one decimal place and zero to N digits before decimal point (e.g. „.5“ or „123.5“).
Following code shows how can be formatted a zero (of double type).
[C#]
String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0);            // "0"
String.Format("{0:#.0}", 0.0);            // ".0"
String.Format("{0:#.#}", 0.0);            // ""

Align numbers with spaces

To align float number to the right use comma „,“ option before the colon. Type comma followed by a number of spaces, e.g. „0,10:0.0“ (this can be used only in String.Format method, not indouble.ToString method). To align numbers to the left use negative number of spaces.
[C#]
String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

Custom formatting for negative numbers and zero

If you need to use custom format for negative float numbers or zero, use semicolon separator „;“ to split pattern to three sections. The first section formats positive numbers, the second section formats negative numbers and the third section formats zero. If you omit the last section, zero will be formatted using the first section.
[C#]
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);  // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0);        // "zero"

Some funny examples

As you could notice in the previous example, you can put any text into formatting pattern, e.g. before an usual pattern „my text 0.0“. You can even put any text between the zeroes, e.g. „0aaa.bbb0“.
[C#]
String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3);          // "12aaa.bbb3"

27 Nisan 2015 Pazartesi

C# – Çapraz İş Parçacığı İşlemi Geçerli Değil Hatası ve Çözümü

Çapraz iş parçacığı işlemi geçerli değil:  denetimine oluşturulduğu iş parçacığı dışında başka bir iş parçacığından erişildi.

BackgroundWorker ile birden fazla thread çalıştırmak istenildiğinde bu hata alınabiliyor. Çözümü ise çok basit;

Aşağıdaki kod satırı InitializeComponent(); satırının üzerine eklenerek sorun giderilmiş oluyor.



 CheckForIllegalCrossThreadCalls = false;

Seri Porttan Hexadecimal (Hex) Değer Göndermek



using (SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8))
        {
            byte[] bytesToSend = new byte[2] { 0x5A, 0x2B };

            port.Open();
            port.Write(bytesToSend, 0, 2);
        }

4 Ocak 2015 Pazar

C# ile Virgülden Sonraki Basamak Sayısını Belirleme

 C# da virgüllü sayılarla işlem yaparken vürgülden sonra belli sayıda basamak göstermek isterseniz aşağıdaki kod satırını kullanmalısınız.
double miktar = 456.1234;
string sonuc = miktar.ToString("0.##");

//Sonuç: 456.12
Yukarıdaki kod bloğu virgülden sonra 2 basamak gösterir. Bunu ("0.##"); satırındaki kod ile sağlıyoruz. Eğer daha fazla basamak göstermek isterseniz # karakter sayısını arttırmalısınız.

11 Aralık 2014 Perşembe

Sql İle Sıralama Yapmak (2000, 2005, 2008 ,2012 ve Tüm versiyonlar)


sp_executesql N' select * from (select top 10 * from (select top 15 * from TblTablo order by Id ASC) a order by Kolon1 DESC) b order by Kolon1 ASC';


Top10 - Ekranda geri dönecek kayıt sayısını gösterir. Top15 ise başlangıç indisini. Bu Sorguda 15. kayıttan başlayarak 10 kayıt getirilmesini sağlamaktadır..

Aynı Kodun C# ile fonksiyon olarak sıralanması;

 private String PrepareQuery(String SiralanacakKolon, Boolean Azalan, Int32 SayfaIndex, Int32 SayfadakiKayit, Int32 KayitSayisi)
        {
            Int32 DonecekKayitSay = SayfadakiKayit;
            Int32 BaslanangicSayisi = SayfadakiKayit * (SayfaIndex + 1);
            if (BaslanangicSayisi > KayitSayisi)
            {
                DonecekKayitSay -= (BaslanangicSayisi - KayitSayisi);
            }
            return String.Format(
            @" sp_executesql N’
            select * from
            (select top {0} * from
            (select top {1} * from Customers order by {2} {3}) a
            order by {2} {4}) b
            order by {2} {3}’;",
            DonecekKayitSay,
            BaslanangicSayisi,
            SiralanacakKolon,
            Azalan ? "ASC" : "DESC",
            Azalan ? "DESC" : "ASC");
         }


9 Eylül 2014 Salı

C# Guid ile Key Üretme

Merhaba arkadaşlar bugün sizlere çok basit bir key üretme metodu olan Guid nasıl kullanılır onu anlatacağım. Çoğu yazılımcının işini görecek çok basit işlev kısaca aşağıdaki gibidir. Ben aşağıdaki kod dizinini metot olarak kullandım isteğe bağlı olarak istediğiniz koşullarda kullanabilirsiniz.
1public static string CreateActivationCode()
2{
3   string Code = Guid.NewGuid().ToString();
4   return Code;
5}


Bu metot ile oluşan key örnekleri aşağıdaki gibidir…
7d87117d-5577-46f5-8d74-81983e1a38af
6825ad18-e591-441b-8882-43a1bb7a3286


Bir başka makalede görüşmek dileğiyle

C# ile TC Kimlik Kontrolu

Merhaba arkadaşlar bugün sizlere çok fazla noktada işimize yarayabilecek olan C#’da TC Kimlik Kontrolü nasıl yapılır sorusuna yanıt verecek metodu paylaşacağım.
*Aşağıda metodun kullanım şeklini göreceğiz.
1//"11111111111" TC kimlik numarası hatalı olduğu için false dönecektir.
2bool result = TCNoKontrolu("11111111111");
3 
4//"10000000146" TC kimlik numarası doğru olduğu için true dönecektir. (TC Kimlik numarası olarak her yönden örnek olabilecek Ulu Önder Mustafa Kemal Atatürk'ün vatandaşlık numarası alınmıştır.)
5bool result = TCNoKontrolu("10000000146");


*TCNoKontrolu isimli metodumuz aşağıdaki gibidir.
01public static bool TCNoKontrolu(string TCNo)
02{
03    int[] TC = new int[11];
04    for (int i = 0; i < 11; i++)
05    {
06        string a = TCNo[i].ToString();
07        TC[i] = Convert.ToInt32(a);
08    }
09 
10    int tekler = 0;
11    int ciftler = 0;
12 
13    for (int k = 0; k < 9; k++)
14    {
15        if (k % 2 == 0)
16            tekler += TC[k];
17        else if (k % 2 != 0)
18            ciftler += TC[k];
19    }
20 
21    int t1 = (tekler * 3) + ciftler;
22    int c1 = (10 - (t1 % 10)) % 10;
23    int t2 = c1 + ciftler;
24    int t3 = (t2 * 3) + tekler;
25    int c2 = (10 - (t3 % 10)) % 10;
26 
27    if (c1 == TC[9] && c2 == TC[10])
28        return true;
29    else
30        return false;
31}


Bir başka makalede görüşmek dileğiyle…