
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
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
- Related Articles
- Formatted string literals (f-strings) in Python?
- What are string literals in C#?
- What is the difference between character literals and string literals in Java?
- What are Perl String Literals?
- Character constants vs String literals in C#
- What are string literals in C language?
- Formatted Output in Java
- Formatted output in C#
- What is the type of string literals in C/ C++?
- What happen if we concatenate two string literals in C++?
- What does the @ prefix do on string literals in C#?
- Getting formatted time in Python
- What is the type of string literals in C and C++?
- Integer literals vs Floating point literals in C#
- How and where does String literals in Java stored in the memory?

Advertisements