
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Convert Class in C#
The Convert Class has methods to convert a base data type to another base data type. Let us see some examples −
The Convert.ToBoolean() method in C# is used to convert a specified value to an equivalent Boolean value.
Syntax
Following is the syntax −
public static bool ToBoolean (string val, IFormatProvider provider);
Above, Val is a string that contains the value of either TrueString or FalseString, whereas the provider is an object that supplies culture-specific formatting information.
Example
Let us now see an example to implement the Convert.ToBoolean() method −
using System; using System.Globalization; public class Demo { public static void Main(){ CultureInfo cultures = new CultureInfo("en-US"); String str = "true"; Console.WriteLine("Converted bool value..."); bool res = Convert.ToBoolean(str, cultures); Console.Write("{0}", res); } }
Output
This will produce the following output −
Converted bool value... True
The Convert.ToDouble() method in C# converts the specified string representation of a number to an equivalent double-precision floating-point number, using the specified culture-specific formatting information.
Syntax
Following is the syntax −
public static double ToDouble (string val, IFormatProvider provider);
Above, the value is a string that contains the number to convert, whereas the provider is an object that supplies culture-specific formatting information.
Example
Let us now see an example to implement the Convert.ToDouble() method −
using System; using System.Globalization; public class Demo { public static void Main(){ String val = "876876, 878"; NumberFormatInfo formatProvider = new NumberFormatInfo(); formatProvider.NumberDecimalSeparator = ", "; formatProvider.NumberGroupSeparator = "."; formatProvider.NumberGroupSizes = new int[] { 2 }; Console.WriteLine("Converted Decimal value..."); double res = Convert.ToDouble(val, formatProvider); Console.Write("{0}", res); } }
Output
This will produce the following output −
Converted Decimal value... 876876.878
The Convert.ToDecimal() method in C# converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information.
Output
Following is the syntax −
public static decimal ToDecimal (string val, IFormatProvider provider);
Above, Val is a string that contains a number to convert, whereas the provider is an object that supplies culture-specific formatting information.
Example
Let us now see an example to implement the Convert.ToDecimal() method −
using System; using System.Globalization; public class Demo { public static void Main(){ CultureInfo cultures = new CultureInfo("en-US"); String val = "8787"; Console.WriteLine("Converted Decimal value..."); decimal res = Convert.ToDecimal(val, cultures); Console.Write("{0}", res); } }
Output
This will produce the following output −
Converted Decimal value... 8787
- Related Articles
- How to convert a class to another class type in C++?
- How to convert a sub class variable into a super class type in Java?
- How to convert a super class variable into a sub class type in Java
- Class in C#
- Debug Class vs Debugger Class in C#
- BigInteger Class in C#
- Stack Class in C#
- Static class in C#
- String class in C#
- Date Class in C#
- DivideByZeroException Class in C#
- Local Class in C++
- Singleton Class in C#
- Pair Class in C#
- Stopwatch class in C#
