Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rounding up and decimal places

Status
Not open for further replies.

aaronjonmartin

Technical User
Jul 9, 2002
475
GB
Hi I have a variable which is of type string, it contains the value 1.676432. I was wondering how can I convert this to a decimal number rounded up to 2 decimal places?

Any help with this would be really appreciated.

Aaron

"It's so much easier to suggest solutions when you don't know too much about the problem."
Malcolm Forbes (1919 - 1990)
 
To convert it to a decimal, you can use any of the following methods:

1) Decimal.TryParse()
2) Cdec()
3) Ctype()

You can then use the Round method of the Decimal object.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for the suggestions ca8msm.

I ended up doing it this way:

Code:
localdec = Convert.ToDecimal(localrate)
localdec = Math.Round(localdec, 2)

"It's so much easier to suggest solutions when you don't know too much about the problem."
Malcolm Forbes (1919 - 1990)
 
Just make sure the input is a numeric value otherwise you'll probably get an error. The Decimal.TryParse is like a try/catch all in one (very useful).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top