?
Solved

Decimals and rounding off

Posted on 2004-09-20
5
Medium Priority
?
380 Views
Last Modified: 2010-08-05
I am trying to calculate the number of pages in a repeater based on the records returned and page size, but am having issue with decimals and rounding off. e.g.

iRecordCount = 18;
iPageSize = 10;

// Get total pages
decimal dPageRecords = Convert.ToDecimal(iRecordCount / iPageSize);
iTotalPages = Convert.ToInt32(System.Math.Round(dPageRecords));

Trace.Warn("dPageRecords=" + dPageRecords.ToString());
Trace.Warn("iTotalPages=" + iTotalPages);

Output I'm looking for is:
dPageRecords=1.8
iTotalPages=2

Whereas I keep fricking getting:
dPageRecords=1
iTotalPages=1

Any ideas thanks?
0
Comment
Question by:jonnyboy69
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 12104622
 int iRecordCount = 18;
  int iPageSize = 10;

  double dPageRecords = (double)iRecordCount/(double)iPageSize;
  int iTotalPages = (int)Math.Ceiling(dPageRecords);

  Trace.Warn("dPageRecords=" + dPageRecords.ToString());
  Trace.Warn("iTotalPages=" + iTotalPages.ToString());

Will yeild the output you desire:

  dPageRecords=1.8
  iTotalPages=2
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12104677
Another note: If you want one and only one decimal in your dPageRecords output, use this:

  Trace.Warn("dPageRecords=" + String.Format("{0:0.0}", dPageRecords));

Examples of output are:

  Value of dPageRecords -> Value of String.Format("{0:0.0}", dPageRecords)
  1.8 -> 1.8
  1.85447 -> 1.9
  1 -> 1.0

  So you will always have 1 decimal displayed.
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12105029
you can use
iTotalPages=(iRecordCount-(iRecordCount%iPageSize))/iPageSize+1
to get the pages
if you'd like to define pages as the page with full pagesize you can ommit +1 as
iTotalPages=(iRecordCount-(iRecordCount%iPageSize))/iPageSize
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12107478
jonnyboy69, I would suggest you use tzxie2000's solution for iTotalPages - it avoids any possibility of a rounding error.

  iTotalPages = iRecordCount/iPageSize + (iRecordCount%iPageSize==0 ? 0 : 1);
0
 
LVL 1

Expert Comment

by:blueforce
ID: 12108218
The problem you've got is an order of operations problem.


This code is all well and good since an int can be implicitly cast "up" to a decimal:

decimal dPageRecords = Convert.ToDecimal(iRecordCount / iPageSize);
iTotalPages = Convert.ToInt32(System.Math.Round(dPageRecords));


However, look at the order of operations
Step 1.  iRecordCount / iPageSize  (both are ints so the answer will be an int - a rounded off int).
Step 2.  Convert the integer answer from Step 1 to a decimal
Step 3.  Assign the new decimal answer to dPageRecords.

It doesn't matter how big or small iRecordCount or iPageSize become - if you want to eventually hold a decimal, you'll have to explicitly cast to some floating type or use decimals to begin with.


The following will work since the casts happen before the division which results in the same datatype as the dividend and divisor:
decimal dPageRecords = Convert.ToDecimal( (decimal)iRecordCount / (decimal)iPageSize);  

Or you could just use decimals to begin with - the trade-off of a decimal datatype vs. the speed of explicitly casting an Int32 is about the same.

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question