How do I limit a value to 2 decimal places?

J

JamesL

Kind of a silly little one for me to be stumped on but:

I do some math on some values that represent money. The math may result in
several decimal places but since it is money I only want to display 2
decimal places on the screen and store 2 decimal places in my text file.
How do I limit the value to 2 decimal places.

This is usually pretty simple but I am stumped. Am I overlooking the
obvious on this one?

James Lysaght
 
E

Earl

I haven't had need to use this in CF, but in the full framework, you simply
do it like so:

SalePrice = Math.Round(yourDecimalValue, 2)
 
S

Sergey Bogdanov

Try this:

decimal d = 123.4567m;
d.ToString("0.00");

or

Math.Round(d, 2).ToString();

or

String.Format("{0:0.00}", d)
 

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

Top