-
Notifications
You must be signed in to change notification settings - Fork 5
Number Formatting
Sann Lynn Htun edited this page Nov 19, 2024
·
1 revision
C# provides a variety of ways to format numbers to make them more readable and to present them in specific formats. This is particularly useful for displaying data to users in a human-readable form, handling currency, percentages, and custom formats.
- Standard Numeric Format Strings
C# offers predefined formats for common scenarios like currency, decimal, and percentages.
- C (Currency): Formats the number as a currency value.
- D (Decimal): Formats the number as a decimal.
- E (Exponential): Formats the number in scientific notation.
- F (Fixed-point): Formats the number as a fixed-point number.
- N (Number): Formats the number with thousand separators.
- P (Percent): Formats the number as a percentage.
using System;
class Program
{
static void Main()
{
double value = 1234.5678;
Console.WriteLine(value.ToString("C")); // Currency
Console.WriteLine(value.ToString("D")); // Decimal
Console.WriteLine(value.ToString("E")); // Exponential
Console.WriteLine(value.ToString("F")); // Fixed-point
Console.WriteLine(value.ToString("N")); // Number
Console.WriteLine(value.ToString("P")); // Percent
}
}
- Custom Numeric Format Strings
You can create custom formats to fit specific needs using special format specifiers.
using System;
class Program
{
static void Main()
{
double value = 1234.5678;
// Custom formatting with "0" as a digit placeholder and "#" as an optional digit
Console.WriteLine(value.ToString("0.00")); // 1234.57
Console.WriteLine(value.ToString("00000")); // 01235
Console.WriteLine(value.ToString("#.##")); // 1234.57
Console.WriteLine(value.ToString("0,0.00")); // 1,234.57
Console.WriteLine(value.ToString("$#,##0.00")); // $1,234.57
}
}
- Culture-Specific Formatting
Numeric formatting can be affected by culture settings, which control aspects like currency symbols, decimal separators, and digit grouping.
using System;
using System.Globalization;
class Program
{
static void Main()
{
double value = 1234.5678;
CultureInfo usCulture = new CultureInfo("en-US");
CultureInfo frCulture = new CultureInfo("fr-FR");
Console.WriteLine(value.ToString("C", usCulture)); // $1,234.57
Console.WriteLine(value.ToString("C", frCulture)); // 1 234,57 €
}
}
- Standard Numeric Format Strings: Common formats like currency, decimal, percentage.
- Custom Numeric Format Strings: Allows for specific, tailored number formatting.
- Culture-Specific Formatting: Adapts number formatting to different cultural settings.
C# number formatting provides powerful tools to present numerical data in a clear and user-friendly way, enhancing the readability and usability of applications.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.