Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3.  
  4. namespace _27_ManipulateStrings
  5. {
  6. public class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. double price = 14.99;
  11. var now = DateTime.Now;
  12.  
  13. var formattedDollarPrice = price.ToString("C", new CultureInfo("en-US", false));
  14. var formattedCurrentCulturePrice = price.ToString("C", CultureInfo.CurrentCulture);
  15.  
  16. Console.WriteLine($"Price (in dollars): {formattedDollarPrice}");
  17. Console.WriteLine($"Price (in local currency): {formattedCurrentCulturePrice}");
  18.  
  19. Console.WriteLine("Date format:");
  20.  
  21. var formatteLongdDate = now.ToString("D"); // equivalent to now.ToLongDateString();
  22. var formattedShortDate = now.ToString("d"); // equivalent to now.ToShortDateString();
  23. var customFormatDate = now.ToString("dd-MM-yyyy");
  24.  
  25. Console.WriteLine("-----Using string.Format-----");
  26. var stringFormatDate = string.Format("Long date: {0:D}, Shortdate: {1:d}, Currency: {2:C}\n", now, now, price);
  27. Console.WriteLine(stringFormatDate);
  28.  
  29. Console.WriteLine("-----Using ToString overloads-----");
  30.  
  31. Console.WriteLine($"Date in LongDate format: {formatteLongdDate}");
  32. Console.WriteLine($"Date in ShortDate format: {formattedShortDate}");
  33. Console.WriteLine($"Date in dd-MM-yyyy format: {customFormatDate}\n");
  34.  
  35. Console.WriteLine("-----Using string interpolation-----");
  36.  
  37. Console.WriteLine($"Long date: {now:D}, Shortdate: {now:d}, Currency: {price:C}");
  38.  
  39. Console.ReadKey();
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement