Last active
October 2, 2019 10:06
-
-
Save luisdeol/ec5da2521063e2b48504e1af020b89e6 to your computer and use it in GitHub Desktop.
2.7: String Formats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
namespace _27_ManipulateStrings | |
{ | |
public class Program | |
{ | |
static void Main(string[] args) | |
{ | |
double price = 14.99; | |
var now = DateTime.Now; | |
var formattedDollarPrice = price.ToString("C", new CultureInfo("en-US", false)); | |
var formattedCurrentCulturePrice = price.ToString("C", CultureInfo.CurrentCulture); | |
Console.WriteLine($"Price (in dollars): {formattedDollarPrice}"); | |
Console.WriteLine($"Price (in local currency): {formattedCurrentCulturePrice}"); | |
Console.WriteLine("Date format:"); | |
var formatteLongdDate = now.ToString("D"); // equivalent to now.ToLongDateString(); | |
var formattedShortDate = now.ToString("d"); // equivalent to now.ToShortDateString(); | |
var customFormatDate = now.ToString("dd-MM-yyyy"); | |
Console.WriteLine("-----Using string.Format-----"); | |
var stringFormatDate = string.Format("Long date: {0:D}, Shortdate: {1:d}, Currency: {2:C}\n", now, now, price); | |
Console.WriteLine(stringFormatDate); | |
Console.WriteLine("-----Using ToString overloads-----"); | |
Console.WriteLine($"Date in LongDate format: {formatteLongdDate}"); | |
Console.WriteLine($"Date in ShortDate format: {formattedShortDate}"); | |
Console.WriteLine($"Date in dd-MM-yyyy format: {customFormatDate}\n"); | |
Console.WriteLine("-----Using string interpolation-----"); | |
Console.WriteLine($"Long date: {now:D}, Shortdate: {now:d}, Currency: {price:C}"); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment