Format Localized numeric input - CSharp Language Basics

CSharp examples for Language Basics:Data Type Format

Description

Format Localized numeric input

Demo Code

using System;/*from w w w. j  ava2 s.  co  m*/
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
class Program
{
   static void Main(string[] args)
   {
      // AMERICAN
      CultureInfo american = new CultureInfo("en-US");
      try
      {
         // Input
         Console.Write("Enter American decimal number: ");
         string input = Console.ReadLine();
         double number = Convert.ToDouble(input, american);
         // Output
         Console.WriteLine("You have entered " + number);
      }
      catch (Exception)
      {
         // Error message
         Console.WriteLine("Incorrect input");
      }
      // CZECH
      CultureInfo czech = new CultureInfo("cs-CZ");
      try
      {
         // Input
         Console.Write("Enter Czech decimal number: ");
         string input = Console.ReadLine();
         double number = Convert.ToDouble(input, czech);
         // Output
         Console.WriteLine("You have entered " + number);
      }
      catch (Exception)
      {
         // Error message
         Console.WriteLine("Incorrect input");
      }
   }
}

Result


Related Tutorials