How to convert culture specific double using TypeConverter?

Posted by Christian on Stack Overflow See other posts from Stack Overflow or by Christian
Published on 2010-04-22T07:29:31Z Indexed on 2010/04/22 7:33 UTC
Read the original article Hit count: 335

Filed under:
|

Hi I have a problem with the TypeConverter class. It works fine with CultureInvariant values but cannot convert specific cultures like english 1000 seperators. Below is a small test program that I cannot get to work.

using System;
using System.Globalization;
using System.ComponentModel;

namespace TestConvertCulture {

  class Program {
    static void Main() {
      try {
        var culture = new CultureInfo( "en" );
        TypeConverter typeConverter = TypeDescriptor.GetConverter( typeof ( double ) );
        double value = (double)typeConverter.ConvertFromString( null, culture, "2,999.95" );
        Console.WriteLine( "Value: " + value );
      }
      catch( Exception e ) {
        Console.WriteLine( "Error: " + e.Message );
      }
    }
  }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about cultureinfo