Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 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 July 6th, 2007, 04:12 PM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Rounding double to integer

Hi,

I am new to C# 2005. I am reading Visual C# 2005 Express Edition and in Chapter 5, the author mentioned that for Rounding (double) correctly, you should use the Convert class. I tried and it DOES NOT work.

double dblValue = 2.5;
int intValueTotal1;
intValueTotal1 = Convert.ToInt32(dblValue);

It works for 2.6-2.9 but does not work for 2.5.
For Rounding 2.5, it should be 3 and NOT 2.

Any help? What should one use for Rounding? Thanks.

Regards,
NewBee
 
Old July 8th, 2007, 12:25 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I have found the answer. Apparently, c# Math.Round uses bankers' rounding .ie.
Math.Round(4.4) 'Returns 4.0.
Math.Round(4.5) 'Returns 4.0.
Math.Round(4.6) 'Returns 5.0.

To use Symmetric Arithmetic Rounding or Round-Half-Up (Symmetric Implementation), which is commonly used, for example in accounting.
4.4 returns 4.0
4.5 returns 5.0
4.6 returns 5.0

A few suggested methods :
(1) To use The Excel Round() spreadsheet function.
(2) The SQL Server Round() function can do symmetric arithmetic rounding.

Regards,
Newbee






Similar Threads
Thread Thread Starter Forum Replies Last Post
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 in C# cjo ASP.NET 1.0 and 1.1 Basics 3 November 3rd, 2003 04:12 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.