Show money from a database

  • Thread starter Thread starter Alberto
  • Start date Start date
A

Alberto

I have a decimal field in a SQL Server DataBase who stores money and I want
to show it in a TextBox and in a ListView controls in C#.

If I do: txtMoney.Text = Convert.ToSingle(dr["money"]);
I see something like: "4.0000" instead "4.00 ?"

How can I solve this?

Thank you very much
 
Alberto,

Basically, you would want to get the currency format for the system, and
then display that. You can do this easily through the ToString method on
the numeric type that is returned.

You don't need to call the static ToSingle method on the Convert class.
Rather, you can just cast it to your type, like so:

// Cast to the value.
// Float is the C# type alias for Single.
float money = (float) dr("money");

// Write in currency format.
txtMoney.Text = money.ToString("C");

The "C" indicates to use the currency format currently defined for the
thread (which is usually the local machine's setting). Just make sure that
the currency that the amount is in is the same as the one on the local
machine. Otherwise, you will have to get the setting for the region, and
then use that.

Hope this helps.
 
Thank you very much. I didn't find anything about this in any book.

Nicholas Paldino said:
Alberto,

Basically, you would want to get the currency format for the system,
and then display that. You can do this easily through the ToString method
on the numeric type that is returned.

You don't need to call the static ToSingle method on the Convert class.
Rather, you can just cast it to your type, like so:

// Cast to the value.
// Float is the C# type alias for Single.
float money = (float) dr("money");

// Write in currency format.
txtMoney.Text = money.ToString("C");

The "C" indicates to use the currency format currently defined for the
thread (which is usually the local machine's setting). Just make sure
that the currency that the amount is in is the same as the one on the
local machine. Otherwise, you will have to get the setting for the
region, and then use that.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Alberto said:
I have a decimal field in a SQL Server DataBase who stores money and I
want to show it in a TextBox and in a ListView controls in C#.

If I do: txtMoney.Text = Convert.ToSingle(dr["money"]);
I see something like: "4.0000" instead "4.00 ?"

How can I solve this?

Thank you very much
 
Back
Top