Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

GetCurrencyFormat (kernel32)
 
.
Summary

C# Signature:

[DllImport("kernel32.dll")]
static extern int GetCurrencyFormat(uint Locale, uint dwFlags, string lpValue,
   [In, MarshalAs(UnmanagedType.LPStruct)]  lpFormat, IntPtr lpCurrencyStr, int cchCurrency);

User-Defined Types:

C# User-Defined Types

    [StructLayout(LayoutKind.Sequential)]
    public class CURRENCYFMT
    {
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
        public UInt32 uiNumDigits;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
        public UInt32 uiLeadingZero;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
        public UInt32 uiGrouping;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        public String lpDecimalSep;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        public String lpThousandSep;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
        public UInt32 uiNegativeOrder;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.U4)]
        public UInt32 uiPositiveOrder;
        [MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)]
        public String lpCurrencySymbol;
    };

Notes:

uiLeadingZero - If there is only a fractional part, is a zero placed in front of the decimal.

    0 => False - Don't show the leading zero.
    1 => True - Show the leading zero.

uiPositiveOrder - How to format the currency if it is positive

    0 => $###.##
    1 => ###.##$
    2 => $ ###.##
    3 => ###.## $

uiNegativeOrder - How to format the currency if it is negative

     0 => ($###.##)
     1 => -$###.##
     2 => $-###.##
     3 => $###.##-
     4 => (###.##$)
     5 => -###.##$
     6 => ###.##-$
     7 => ###.##$-
     8 => -###.## $
     9 => -$ ###.##
    10 => ###.## $-
    11 => $ ###.##-
    12 => $ -###.##
    13 => ###.##- $
    14 => ($ ###.##)
    15 => (###.## $)

Tips & Tricks:

Please add some!

Sample Code:

C# Example 1

    /// <summary>
    /// Will convert a decimal to the specified currency format.
    /// </summary>
    /// <param name="numberToConvert">Must be a decimal number with no formatting.  If there is formatting
    /// on the number, it should be removed before calling this function.</param>
    /// <returns>Null if there is an error, otherwise the number formatted as currency.</returns>
    public string ConvertDecimalToCurrency(Decimal numberToConvert, CURRENCYFMT currencyFormat)
    {
        String sCurrency = null;
        long lRet = 0;

        //Find out the size of the converted string..
        lRet = GetCurrencyFormat(0, 0, numberToConvert.ToString(), currencyFormat, IntPtr.Zero, 0);

        if (0 != lRet)
        {
        //Get the formatted currency string

        IntPtr ptrCurrencyStr = Marshal.AllocHGlobal((int)lRet);

        lRet = GetCurrencyFormat(0, 0, numberToConvert.ToString(), currencyFormat, ptrCurrencyStr, (int)lRet);

        sCurrency = 0 == lRet ? null : Marshal.PtrToStringUni(ptrCurrencyStr);

        Marshal.FreeHGlobal(ptrCurrencyStr);
        }
        else
        {
        //Shouldn't be here, we should have received the size of the converted string.
        //See the error code that was returned.
        int error = Marshal.GetLastWin32Error();
        Trace.WriteLine("Error converting decimal to currency.  Error Number: " + error.ToString());
        }

        return sCurrency;
    }

    private void cmdTest_Click(object sender, EventArgs e)
    {
        //Setup how the currency is going to be formatted.
        CURRENCYFMT currencyFormat = new CURRENCYFMT();
        currencyFormat.uiGrouping = Convert.ToUInt32(txtGrouping.Text);
        currencyFormat.uiLeadingZero = Convert.ToUInt32(cboLeadingZero.SelectedIndex);
        currencyFormat.lpCurrencySymbol = txtCurrencySymbol.Text;
        currencyFormat.lpDecimalSep = txtDecimalSep.Text;
        currencyFormat.lpThousandSep = txtThousandSep.Text;
        currencyFormat.uiNegativeOrder = Convert.ToUInt32(cboNegativeOrder.SelectedIndex);
        currencyFormat.uiNumDigits = Convert.ToUInt32(txtNumDigits.Text);
        currencyFormat.uiPositiveOrder = Convert.ToUInt32(cboPositiveOrder.SelectedIndex);

        txtConverted.Text = ConvertDecimalToCurrency(Convert.ToDecimal(txtConvert.Text), currencyFormat);
    }

Alternative Managed API:

You may be able to use this, I don't know how much customization you can get from it.

Do you know one? Please contribute it!

Look at this link: http://msdn.microsoft.com/en-us/goglobal/bb688126.aspx

  Decimal n = 100.53m;
  string sFormatted = n.ToString("C");

Documentation

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions