Totzkeeeeee's Blog

Just because I can...

  Home  |   Contact  |   Syndication    |   Login
  210 Posts | 4 Stories | 345 Comments | 321 Trackbacks

News


My blog is worth $14,678.04.
How much is your blog worth?

Tag Cloud


Article Categories

Archives

Post Categories

Image Galleries

Blog Roll

Cool Sites

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

Feedback

# re: HOWTO: IFormatProvider, Culture and ToString() 9/12/2005 2:02 PM Curt Teunissen
Thanks, simple example but makes the documentation make sense now.

# re: HOWTO: IFormatProvider, Culture and ToString() 9/6/2007 5:02 AM Kerem Ispirli
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!

# re: HOWTO: IFormatProvider, Culture and ToString() 10/22/2008 2:29 AM mustafa
great post, thank you

# re: HOWTO: IFormatProvider, Culture and ToString() 11/13/2008 9:12 PM Allan
Thank You!

# re: HOWTO: IFormatProvider, Culture and ToString() 2/17/2011 10:42 AM Patrick barry
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);
}
}

Comments have been closed on this topic.