14

I am querying a database field that returns a money value, I am assigning this to a string but it is adding extra 00 on the end.

e.g. Query returns 30.00

I assign this to a string (string value = Convert.ToString(ReturnValue);) but when I output it, it is showing as 30.0000

Can you advise me where I'm going wrong and the best way to approach this?

Blorgbeard
  • 97,316
  • 47
  • 222
  • 267
MartGriff
  • 2,751
  • 7
  • 37
  • 42

10 Answers10

15

MartGriff,

My best advice would be to convert it to a double using the SqlMoney type. From there, you can output it however you would like!

Here's an example:

System.Data.SqlTypes.SqlMoney ReturnValue;

//Set your returnValue with your SQL statement
ReturnValue = ExecuteMySqlStatement();

//Get the format you want

//$30.00
string currencyFormat = ReturnValue.ToDouble().ToString("c");

//30.00
string otherFormat = ReturnValue.ToDouble().ToString("0.00");

For more formatting options, check out the MSDN:

http://msdn.microsoft.com/en-us/library/system.double.tostring.aspx

Best of luck, I hope this helps.

Snickers
  • 1,583
  • 10
  • 6
11

You can use string format codes in your ToString call.

Ed S.
  • 119,398
  • 20
  • 176
  • 254
6

Do you want your string formatted using a currency character?

If so...

decimal m = 3.4;

string s = string.Format("{0:c}", m);

// s will be £3.40, $3.40, etc depending on your locale settings
Richard Ev
  • 51,030
  • 56
  • 187
  • 275
3

I'd use something like

string value = ReturnValue.ToString("0.00");

This uses the ToString overload that accepts a format string. The above format string "0.00" specifies two decimal places.

Scott Ferguson
  • 7,490
  • 7
  • 39
  • 64
2

I am currently developing a boatdock web system, using VS 2013 Community edition. I was trying to work out how to output a currency value into a textbox.

The method that I used was

fieldName.Text = decimalValue.ToString("c");

It works perfectly.

1

My recommendation is to convert it to Decimal firstly. I'd use something like this:

string value = (Convert.ToDecimal(ReturnValue)).ToString("0,0.00");
Muhammad Musavi
  • 2,186
  • 2
  • 19
  • 33
1

Try this:

yourValueHere.ToString("0.00")
Jay Riggs
  • 52,110
  • 9
  • 138
  • 148
0
string value = (Convert.ToDecimal(ReturnValue)).ToString("c");

c is for currency p for percentage. There is a list of other letters you can use.

0

In SQLServer a money data type is the equivalent of a decimal with 4 decimal places of precision. I would assume this is precision is conserved when the decimal is converted to a string. The best option would be to always use a format string eg "#,##0.00" when doing the conversion.

bstoney
  • 6,258
  • 5
  • 43
  • 51
0

The Money data type has a precision of 4 decimal places. You'll need to either specify the format in the ToString() arguments or you can round off the value before converting to a string.

If you use .ToString() you're not getting any rounding as far as I know. I think you just lose the digits.

If you round, you're not just chopping off digits. Here's a sample for rounding (untested code):

string value = Math.Round(ReturnValue, 2).ToString();
Ian Suttle
  • 3,354
  • 2
  • 22
  • 27