Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than kernel32, prefix the name with the module name and a period.
GetCurrencyFormat (kernel32)
.
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
/// <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
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);
Decimal n = 100.53m;
string sFormatted = n.ToString("C");
The GetCurrencyFormat API
10/27/2009 5:41:18 PM - Ken G-216.57.222.134
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).