Decimal parse

In this chapter you will learn:

  1. How to parse decimal from string
  2. Decimal parse for currency
  3. Parse string using "$" as the currency symbol for en-GB and en-us cultures.
  4. Parse string using "." as the thousands separator and " " as the decimal separator.
  5. Parse a floating-point value with a thousands separator.

Parse a string to decimal

using System;  //  j av a  2s .com
  
class MainClass {     
  public static void Main() {     
    try {  
      decimal d = Decimal.Parse("1234.1234");  
      Console.WriteLine(d);  
    } catch(FormatException exc) {  
      Console.WriteLine(exc.Message);  
      return;  
    }  
  }
}

The code above generates the following result.

Decimal parse for currency

using System;/* jav  a2 s  . c  o m*/
using System.Globalization;
using System.Text;


public class MainClass{
   public static void Main(){
        string[] money = new string[] { "$0.99", "$0,99", 
                  "$1000000.00", "$10.25", "$90,000.00", 
                  "$90.000,00", "$1,000,000.00", "$1,000000.00" };
        foreach (string m in money)
        {
            try{
                Decimal.Parse(m, NumberStyles.Currency);
                Console.WriteLine("!{0}: True", m);
            }catch (FormatException){
                Console.WriteLine("!{0}: False", m);
            }

        }
   }
}

The code above generates the following result.

Parse string using "$" as the currency symbol for en-GB and en-us cultures.

using System;// j  a v  a 2 s.co m
using System.Globalization;
class MainClass
{
    public static void Main()
    {
        string value = "$6,543.21";
        NumberStyles style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
        CultureInfo provider = new CultureInfo("en-GB");

        try
        {
            decimal number = Decimal.Parse(value, style, provider);
            Console.WriteLine("'{0}' converted to {1}.", value, number);
        }
        catch (FormatException)
        {
            Console.WriteLine("Unable to parse '{0}'.", value);
        }
    }
}

Parse string using "." as the thousands separator and " " as the decimal separator.

using System;/*  j a v  a2 s  . c  om*/
using System.Globalization;
class MainClass
{
    public static void Main( )
    {
        
        
        string value = "987 654,321";
        NumberStyles style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
        CultureInfo provider = new CultureInfo("fr-FR");
        decimal number = Decimal.Parse(value, style, provider);  
        Console.WriteLine("'{0}' converted to {1}.", value, number);
    
    }
}

Parse a floating-point value with a thousands separator.

using System;/*ja  v  a 2  s. c  om*/

class MainClass
{
    public static void Main( )
    {
        string value;
        decimal number;
        
        // Parse a floating-point value with a thousands separator.
        value = "1,234.56";
        if (Decimal.TryParse(value, out number))
           Console.WriteLine(number);
        else
           Console.WriteLine("Unable to parse '{0}'.", value);      
    }
}

Next chapter...

What you will learn in the next chapter:

  1. Format decimal with currency format
  2. Format Decimal using culture-specific format information
Home » C# Tutorial » Decimal, Float, Double
decimal
decimal calculation
decimal creation
decimal values
decimal flooring and ceiling
decimal conversion
Decimal parse
decimal format
decimal round
double
double type
Predefined double values
double infinit value
Calculation with double value
Parse string to double value
double value format
double vs decimal
Floating-Point Types
float constant value
float calculation
Parse string to float
float format