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

การจัดรูปแบบ String ด้วยเครื่องหมายเปอร์เซ็นต์ (%) ใน C#

ใช้ String.Format เพื่อจัดรูปแบบสตริงด้วยเครื่องหมายเปอร์เซ็นต์ (%) ซึ่งจะคูณค่าด้วย 100 และต่อเครื่องหมาย % ไปด้านหลัง

สมมติค่าของเราคือ:

double val = .322;

ใช้ String.Format และจัดรูปแบบ:

string.Format("string = {0:0.0%}", val);

โค้ดตัวอย่าง

using System;

public class Program
{
    public static void Main()
    {
        double val = .322;
        string res = string.Format("string = {0:0.0%}", val);
        Console.WriteLine(res);
    }
}

ผลลัพธ์

string = 32.2%