C#에서 문자열 리터럴의 형식을 지정하려면 String.Format 메서드를 사용합니다.
다음 예에서 0은 문자열 값이 해당 특정 위치에 삽입되는 개체의 인덱스입니다. -
using System;
namespace Demo {
class Test {
static void Main(string[] args) {
decimal A = 15.2 m;
string res = String.Format("Temperature = {0}°C.", A);
Console.WriteLine(res);
}
}
} 다음 예에서는 이중 유형에 대한 형식 문자열을 지정합니다.
예
using System;
class Demo {
public static void Main(String[] args) {
Console.WriteLine("Three decimal places...");
Console.WriteLine( String.Format("{0:0.000}", 987.383));
Console.WriteLine( String.Format("{0:0.000}", 987.38));
Console.WriteLine(String.Format("{0:0.000}", 987.7899));
Console.WriteLine("Thousands Separator...");
Console.WriteLine(String.Format("{0:0,0.0}", 54567.46));
Console.WriteLine(String.Format("{0:0,0}", 54567.46));
}
} 출력
Three decimal places... 987.383 987.380 987.790 Thousands Separator... 54,567.5 54,567