469,591 Members | 2,064 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,591 developers. It's quick & easy.

Show money from a database

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
Nov 16 '05 #1
2 4599
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]
- mv*@spam.guard.caspershouse.com
"Alberto" <al*****@nospam.es> wrote in message
news:Oo**************@TK2MSFTNGP14.phx.gbl...
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

Nov 16 '05 #2
Thank you very much. I didn't find anything about this in any book.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> escribió
en el mensaje news:eC**************@tk2msftngp13.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"Alberto" <al*****@nospam.es> wrote in message
news:Oo**************@TK2MSFTNGP14.phx.gbl...
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


Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Robby Russell | last post: by
12 posts views Thread by David Garamond | last post: by
14 posts views Thread by Ina Schmitz | last post: by
3 posts views Thread by Alberto | last post: by
45 posts views Thread by Master Programmer | last post: by
5 posts views Thread by Ronald S. Cook | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.