using System; public class FormatSpecApp { public static void Main(string[] args) { int i = 123456; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // 123456 Console.WriteLine("{0:#0;(#0)}", i); // 123456 Console.WriteLine("{0:#0;(#0);<zero>}", i); // 123456 Console.WriteLine("{0:#%}", i); // 12345600% i = -123456; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // -123456 Console.WriteLine("{0:#0;(#0)}", i); // (123456) Console.WriteLine("{0:#0;(#0);<zero>}", i); // (123456) Console.WriteLine("{0:#%}", i); // -12345600% i = 0; Console.WriteLine(); Console.WriteLine("{0:#0}", i); // 0 Console.WriteLine("{0:#0;(#0)}", i); // 0 Console.WriteLine("{0:#0;(#0);<zero>}", i); // <zero> Console.WriteLine("{0:#%}", i); // % } }
Author: coder
Use #, % and
Use CultureInfo in int.ToString method
using System; using System.Globalization; public class NumParsingApp { public static void Main(string[] args) { int k = 12345; CultureInfo us = new CultureInfo("en-US"); string v = k.ToString("c", us); Console.WriteLine(v); } }
Format int in Console.WriteLine
using System; public class TestConsoleApp { public static void Main(string[] args) { Console.WriteLine(123); Console.WriteLine("{0}", 123); Console.WriteLine("{0:D3}", 123); } }
Do calculation with int variable
using System; public class MainClass { static void Main(string[] args) { int a; a = -42; Console.WriteLine("{0}", a); int b = 2; int c = 42; a = b * -c; Console.WriteLine("{0}", a); } }
Catch OverflowException Exception
using System; class MainClass { public static void Main() { try { checked { int Integer1; int Integer2; int Sum; Integer1 = 9999999999; Integer2 = 2000000000; Sum = Integer1 + Integer2; } } catch(OverflowException) { Console.WriteLine("A mathematical operation caused an overflow."); } } }
Integer OverFlow
using System; class IntegerOverFlow { static void Main(string[] args) { short s = 32767; s++; Console.WriteLine(s); ushort us = 0; us--; Console.WriteLine(us); } }
Use CultureInfo int ToString method
using System; using System.Globalization; using System.Threading; class Program { static void Main(string[] args) { int val = 1234567890; Console.WriteLine(val.ToString("N")); Console.WriteLine(val.ToString("N",new CultureInfo("fr-FR"))); Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE"); Console.WriteLine(val.ToString("N")); } }