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

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.
 
M

Martin CLAVREUIL

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 :
 
R

RB

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top