Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old January 10th, 2005, 11:08 AM
Registered User
 
Join Date: Jan 2005
Location: Bangalore, Karnataka, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Some of Time Values

Hi,

I am trying to total the sum of n number of times. In my project the customer will enter the time with a decimal point instead of colon.
I am doing the following:
a) First converting to minutes values and adding all the values and then converting the result back to HH.mm format.

While doing so I am finding a strange problem. Can anyone help me???

When I was trying to subtract (0.57 - 0) I was getting the result as 0.569999 which later rounding to just 0.56. This is happening even for 0.58. But for the other numbers i am getting correct result.

Is there any other easier way to sum the time values perfectly?


Aijaz
Reply With Quote
  #2 (permalink)  
Old January 10th, 2005, 07:12 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How are you rounding the number and what type are you storing the values in?

0.569999 should round properly to 0.57


Kep.
Reply With Quote
  #3 (permalink)  
Old January 23rd, 2005, 03:05 PM
Registered User
 
Join Date: Jan 2005
Location: Bangalore, Karnataka, India.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Since the customer will enter the values in text boxes, Its in string value.
For example: textBox1.Text = ".57" textBox2.text = "" etc which has to be added and display the resume in another textBox.
I am first parsing the textBox value to float.
Then typecasting the int part as Hr and subtracting the full float value with Hr to get the minutes as (0.57 - 0). But I am getting the result as 0.569999 and then by using Decimal.Round(T1.ToString(),2) I am getting 0.56 instead of 0.57.

Aijaz
Reply With Quote
  #4 (permalink)  
Old January 23rd, 2005, 06:24 PM
Kep Kep is offline
Authorized User
 
Join Date: Aug 2003
Location: Melbourne, VIC, Australia.
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The reason that Decimal.Round(0.5699999, 2) rounds to 0.56 is because the Decimal.Round method uses a technique called rounding to even (or rounding to nearest).

This use of rounding ensures that the number always rounds to the nearest even number (at least that's what it says in the help).

I recommend you try using Math.Round(0.569999, 2). This method rounds the way you are expecting (I think).

Kep.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
passing date time values to sql ashokparchuri ADO.NET 4 March 15th, 2005 01:47 AM
How to retrieve multiple values at run time using anshul21 Crystal Reports 1 May 31st, 2004 10:36 PM
Adding two time values j_karthik72 VBScript 1 April 5th, 2004 06:43 AM
DataList original values at update time MColeman ASP.NET 1.x and 2.0 Application Design 2 February 19th, 2004 03:23 PM
Adding time values scott Access 8 January 11th, 2004 05:39 AM



All times are GMT -4. The time now is 09:01 AM.


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