Real Simple Question

  • Thread starter Thread starter Mark Fox
  • Start date Start date
M

Mark Fox

Hello,

I have a "smallmoney" field in SQL Server that I am
attempting to display, but when I use the .ToString()
method on a decimal variable it displays too many places
after the decimal point (i.e. more than two). I know
there's a string I could use as an argument for ToString
() that will only display the first two deigits after the
decimal point, does anyone know what that is? Thanks for
your help!

decimal AccountTotal = 4.7300;

string myString = AccountTotal.ToString("????");
 
The .#### is normal for money types in SQL Server. For output in an ASP.NET
app, you can use DataBinder.Eval to output. The number will still have 4,
but only show 2.

<%# DataBinder.Eval(Container.DataItem, "Price", "{0:c}") %>

This involves late binding, and may not work for all situations. Now, if you
wish to include the $ or other country formatting, you can do this trick:

CurrencyLabel.Text = doubleVal.ToString("C",
Thread.CurrentThread.CurrentCulture);

If it is always dollars, you can use:

CurrencyLabel.Text = doubleVal.ToString("C", new CultureInfo("en-us"));

NOTE that I am using a double here. If you use
System.Data.SqlTypes.SqlMoney, you will ALWAYS have a precision of 4.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

**********************************************************************
Think Outside the Box!
**********************************************************************
 
Gregory,

Thank you so much for the great explanation! It is
extremely helpful. I am using early binding, so the
DataBinder won't work, but how would I get it to display
the amount to two decimal places without the "$"? Thanks
so much for your help!
-----Original Message-----
The .#### is normal for money types in SQL Server. For output in an ASP.NET
app, you can use DataBinder.Eval to output. The number will still have 4,
but only show 2.

<%# DataBinder.Eval
(Container.DataItem, "Price", "{0:c}") %>
 
Hi Mark,

You can consider of the String.Format method. For example,

String.Format(AccountTotal.ToString(), "{0:c}");

If I have misunderstood your concern, please feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top