Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

เมธอด Decimal.ToString() ใน C#

เมธอด Decimal.ToString() ใน C# ใช้แปลงค่าตัวเลขของอินสแตนซ์นี้เป็นการแสดงผลสตริงที่เทียบเท่า

ไวยากรณ์

public override string ToString ();

ตัวอย่าง

ตัวอย่างที่ 1: แปลงค่าทศนิยมเป็นสตริง

using System; 
public class Demo{ 
    public static void Main(){ 
        decimal d = 3444.787m; 
        string str = d.ToString(); 
        Console.WriteLine("String = " + str); 
    } 
}

ผลลัพธ์

String = 3444.787

ตัวอย่างเพิ่มเติม

ตัวอย่างที่ 2: แปลงจำนวนเต็มในรูปแบบทศนิยม

using System; 
public class Demo{ 
    public static void Main(){ 
        decimal d = 100; 
        string str = d.ToString(); 
        Console.WriteLine("String = " + str); 
    } 
}

ผลลัพธ์

String = 100