Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Formatted string literals in C#
To format string literals in C#, use the String.Format method.
In the following example, 0 is the index of the object whose string value gets inserted at that particular position −
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);
}
}
}
In the following example, let us format string for double type.
Example
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));
}
}
Output
Three decimal places... 987.383 987.380 987.790 Thousands Separator... 54,567.5 54,567
Advertisements