Simple Convert String to Decimal with 2 places to the right.

  • Thread starter Thread starter pvong
  • Start date Start date
P

pvong

Newbie learning VB.Net.

I have a simple DataReader and I can grab the info. The data is in numeric
format like 123.99 and I want a TextBox to just display it just like that.
When I use the code below, I get 123.9900 and I don't know how to get rid of
the last 2 zeros. I thought for sure the below would work but it didn't.
Thanks in advance.
 
Hi,

The 'string ToString(string format)' method allready provides the same
fonctionality. In C# it looks likr this :

decimal d = (decimal)123.9900;
Console.WriteLine(d.ToString("N2"));

pvong write :
 
Reading between the lines of your question, and in addition to Martin's
answer, it looks like you are getting a field from a database, which has
been declared as "money", and you want to display it as a currency.

Firstly, a lot of people don't use the "money" datatype, and instead use
something along the lines of "decimal (12, 2)", as they find it easier
to work with - google about why the money datatype is evil will get lots
of argument (for and against!!)

Secondly, you can actually format a number as a currency. Using Martin's
example:

decimal d = (decimal)123.9900;
Console.WriteLine(d.ToString("c"));

As I say, I'm reading between the lines, so apologies if this is not
relevant!

HTH,

Rowland.
 
Back
Top