jonnyboy69
asked on
Decimals and rounding off
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(iRecordC ount / iPageSize);
iTotalPages = Convert.ToInt32(System.Mat h.Round(dP ageRecords ));
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?
iRecordCount = 18;
iPageSize = 10;
// Get total pages
decimal dPageRecords = Convert.ToDecimal(iRecordC
iTotalPages = Convert.ToInt32(System.Mat
Trace.Warn("dPageRecords="
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can use
iTotalPages=(iRecordCount- (iRecordCo unt%iPageS ize))/iPag eSize+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- (iRecordCo unt%iPageS ize))/iPag eSize
iTotalPages=(iRecordCount-
to get the pages
if you'd like to define pages as the page with full pagesize you can ommit +1 as
iTotalPages=(iRecordCount-
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);
iTotalPages = iRecordCount/iPageSize + (iRecordCount%iPageSize==0
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(iRecordC ount / iPageSize);
iTotalPages = Convert.ToInt32(System.Mat h.Round(dP ageRecords ));
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.
This code is all well and good since an int can be implicitly cast "up" to a decimal:
decimal dPageRecords = Convert.ToDecimal(iRecordC
iTotalPages = Convert.ToInt32(System.Mat
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.
Trace.Warn("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.