Implement Format for different Culture Information : Culture Format « I18N Internationalization « C# / CSharp Tutorial






using System;
using System.Globalization;

public sealed class ComplexNumber : IFormattable
{
    public ComplexNumber( double real, double imaginary ) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public override string ToString() {
        return ToString( "G", null );
    }

    public string ToString( string format, IFormatProvider formatProvider ) {
        string result = "(" + real.ToString(format, formatProvider) + " " + real.ToString(format, formatProvider) + ")";
        return result;
    }
   
    private readonly double real;
    private readonly double imaginary;
}

public sealed class MainClass
{
    static void Main() {
        ComplexNumber num1 = new ComplexNumber( 1.12345678, 2.12345678 );
        
        Console.WriteLine( "US format: {0}", num1.ToString( "F5", new CultureInfo("en-US") ) );
        Console.WriteLine( "DE format: {0}", num1.ToString( "F5", new CultureInfo("de-DE") ) );
        Console.WriteLine( "Object.ToString(): {0}",num1.ToString() );
    }
}








21.6.Culture Format
21.6.1.Use current culture to format currency
21.6.2.Implement Format for different Culture Information
21.6.3.Get datetime formatting info from a CultureInfo
21.6.4.Get numeric formatting info from a CultureInfo
21.6.5.Save CultureInfo, its DateTimeFormatInfo and NumberFormatInfo to a text file
21.6.6.Culture formatting: en-US
21.6.7.Culture formatting: en-GB
21.6.8.An culture-friendly money matching expression
21.6.9.Culture-specific formatting: en-US, en-GB, ja, ar
21.6.10.Culture information: DisplayName, NativeName and LCID
21.6.11.Culture information: primary Calendar and optional Calendar
21.6.12.Culture information: Days
21.6.13.Culture information: Months
21.6.14.Culture information: Long Date Format and Short Date Format
21.6.15.Culture information: Currency Format
21.6.16.Culture information: Number Format
21.6.17.Culture information: Number Group Separator