Geeks With Blogs
Totzkeeeeee's Blog Just because I can...

You want to display a value as currency using the format of the culture of the local machine.  The documentation is not too clear on how you use this stuff so I have a little sample that I managed to get working.  Here you go:

In C#:

using System;
using System.Globalization;

namespace ConsoleApplication1
{

    class Class1
   
{
       
[STAThread]
       
static void Main(string[] args)
       
{
            NumberFormatInfo numberInfo = CultureInfo.CurrentCulture.NumberFormat;
            double myAmount = 10.815;
            Console.WriteLine(Math.Round(myAmount, 2).ToString("c", numberInfo));
            Console.ReadLine();

        }
    }
}

In VB:

Imports System.Globalization

 

 

Module Module1

 

    Sub Main()

 

        Dim numberInfo As NumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat

        Dim myAmount As Double = 10.815

 

        Console.WriteLine(Math.Round(10.815, 2).ToString("c", numberInfo))

        Console.ReadLine()

 

 

    End Sub

 

End Module

Posted on Monday, February 28, 2005 7:10 PM | Back to top


Comments on this post: HOWTO: IFormatProvider, Culture and ToString()

# re: HOWTO: IFormatProvider, Culture and ToString()
Requesting Gravatar...
Thanks, simple example but makes the documentation make sense now.
Left by Curt Teunissen on Sep 12, 2005 2:02 PM

# re: HOWTO: IFormatProvider, Culture and ToString()
Requesting Gravatar...
this post gave great help to understand what was and how to use damn iFormatProvider. it's important especially when working with ms office files (e.g. excel) on a windows with culture info other than en-us. Thanks!
Left by Kerem Ispirli on Sep 06, 2007 5:02 AM

# re: HOWTO: IFormatProvider, Culture and ToString()
Requesting Gravatar...
great post, thank you
Left by mustafa on Oct 22, 2008 2:29 AM

# re: HOWTO: IFormatProvider, Culture and ToString()
Requesting Gravatar...
Thank You!
Left by Allan on Nov 13, 2008 9:12 PM

# re: HOWTO: IFormatProvider, Culture and ToString()
Requesting Gravatar...
Great this is my slight variation
public static string CulturedCurrency(object obj,string culture = "en-US")
{
try
{
NumberFormatInfo numberInfo = CultureInfo.CreateSpecificCulture(culture).NumberFormat;
decimal number = WebSite.ParseDecimal(obj);
return number.ToString("c",numberInfo);
}
catch (FormatException ex)
{
Logging.WriteError(ex,"CulturedCurrency number is: " + obj);
return WebSite.ToString(obj);
}
}
Left by Patrick barry on Feb 17, 2011 10:42 AM

Comments have been closed on this topic.
Copyright © David Totzke | Powered by: GeeksWithBlogs.net