Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 3rd, 2003, 02:03 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default Rounding in C#

I'd like to apologize in advance for these questions. I know they're incredibly basic.

Using ASP.NET with C#, can somebody please tell me how to round a result either using a ASP.NET rounding method or a C# operation (or both)?

For example, what is the easiest and most appropriate way to convert a decimal value of 113.9764995 as 113.98 BEFORE assigning it to my variable?

Also, if 113.9764995 is saved in my variable, using ASP.NET, how can I DISPLAY it as 113.98 without actually changing the value in my variable?

Finally, I'd be very appreciative if anybody can refer me to either a C# or ASP.NET resource (or resources) of conversion functions to perform such tasks. I've searched high and low on a number of sites for the answer to these questions, but haven't had any luck.
 
Old November 3rd, 2003, 02:45 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Don't apologize, this is the Beginnings forum after all. :)

Take a look at the System.Math class at the MSDN Library .Net Framework Reference site for the details of all the math methods you can perform including System.Math.Round(). I think the specific one you want is here. You should be able to get all the documentation you need from there.

If I may offer a correction to your terminology. When doing something like rounding, you are not really doing a conversion. Converting implies a change of data type. You could certainly convert a manipulated number if you so choose but you don't need to.

To answer your last question, something like this should work:

lblMyResultLabel.Text = Math.Round(myDecimal, 2).ToString(); //Round to 2 places


Peter
----------------------------------------
Work smarter, not harder.
 
Old November 3rd, 2003, 02:47 PM
Authorized User
 
Join Date: Jul 2003
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont recall what the exact syntax is for rounding but one thing to note is that .Net makes use of Bankers Rounding. There is an older thread on round and the effects of bankers rounding in the archives. I would suggest taking a look at that. If this form of rounding does not give you the results that you are looking for you may need to build your own rounding class.

 
Old November 3rd, 2003, 04:12 PM
cjo cjo is offline
Authorized User
 
Join Date: Oct 2003
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Beautiful! Thanks to both of you. I actually came across many discussions of "Banker's Rounding" when performing searches to find the answer to this problem. Good to know.

Peter, thanks for your help ... especially drilling into the MSDN site for me. I know about this resource, but navigating it has been difficult. I hope to be able to find my way around in due time.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Rounding problem rajanikrishna Classic ASP Basics 1 August 17th, 2006 12:14 AM
Rounding JasperGIS Beginning VB 6 1 December 14th, 2005 03:09 PM
Rounding kilika SQL Server 2000 4 June 1st, 2005 03:36 PM
What's with the rounding?? kaizer BOOK: Beginning Java 2 2 December 22nd, 2003 11:36 PM
Rounding Droopy Classic ASP Basics 3 August 14th, 2003 08:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.