
- 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 output in C#
To format output in C#, let us see examples to format date and double type.
Set formatted output 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
Set formatted output for DateTime
Example
using System; static class Demo { static void Main() { DateTime d = new DateTime(2018, 2, 8, 12, 7, 7, 123); Console.WriteLine(String.Format("{0:y yy yyy yyyy}", d)); Console.WriteLine(String.Format("{0:M MM MMM MMMM}", d)); Console.WriteLine(String.Format("{0:d dd ddd dddd}", d)); } }
Output
18 18 2018 2018 2 02 Feb February 8 08 Thu Thursday
- Related Articles
- Formatted Output in Java
- Getting formatted time in Python
- Formatted string literals in C#
- Formatted text in Linux Terminal using Python
- Formatted string literals (f-strings) in Python?
- Formatted Strings Using Template Strings in JavaScript
- How to get formatted date and time in Python?
- How to get formatted JSON in .NET using C#?
- How to print a formatted text using printf() method in Java?\n
- Output Devices in Computer
- Output Iterators in C++
- Why does std::getline() skip input after a formatted extraction?
- How to obtain the same font in Matplotlib output as in LaTex output?
- Cardiac Output
- Program to find the formatted amount of cents of given amount in Python

Advertisements