Wednesday, July 29, 2009

Daily Expense Tracker

I have made a small daily expense tracker using excel which can be used for managing daily expenses recurred to you every month.
The main attraction of this tracker is a graph which will plot your daily expenses along with average daily expense for the month.

Hope this will be help full to at least a bunch of people who care about keeping rack of their day to day expenses.
As blogger site wont allow us to upload any files otherthan images or video i have uploaded the same in MegaFileUpload site.
Daily Expenses.xls

Friday, July 17, 2009

Round a Large Decimal Number in Javascript

There might be a situation where you need to round a very large decimal number in JavaScript. But using the built-in JavaScript methods you wont be able to get it done with the precision you want to get. So here i am trying to give you a method to round any digits of decimal number with the precision you want to get.


/**
* Method to round decimal numbers
* number - The number that must be rounded
* decimals - The number of decimal points that should appear in the result
*/
function roundNumber(number, decimals)
{
//return roundNumber(number,decimals);
var newString;// The new rounded number
decimals = Number(decimals);
if (decimals < 1) {
newString = (Math.round(number)).toString();
} else {
var numString = number.toString();
if(numString.trim().length == 0) {
numString = "0.00";
}
if(numString.indexOf(".") == 0) {
numString = "0" + numString;
}
if(parseFloat(numString) == 0) {
numString = "0.00";
}
if (numString.lastIndexOf(".") == -1) {// If there is no decimal point
numString += ".";// give it one at the end
}
var cutoff = numString.lastIndexOf(".") + decimals;// The point at which to truncate the number
var d1 = Number(numString.substring(cutoff,cutoff+1));// The value of the last decimal place that we'll end up with
var d2 = Number(numString.substring(cutoff+1,cutoff+2));// The next decimal, after the last one we want
if (d2 >= 5) {// Do we need to round up at all? If not, the string will just be truncated
if (d1 == 9 && cutoff > 0) {// If the last digit is 9, find a new cutoff point
while (cutoff > 0 && (d1 == 9 || isNaN(d1))) {
if (d1 != ".") {
cutoff -= 1;
d1 = Number(numString.substring(cutoff,cutoff+1));
} else {
cutoff -= 1;
}
}
}
d1 += 1;
}
newString = numString.substring(0,cutoff) + d1.toString();
}
if (newString.lastIndexOf(".") == -1) {// Do this again, to the new string
newString += ".";
}
var decs = (newString.substring(newString.lastIndexOf(".")+1)).length;
for(var i=0;i<decimals-decs;i++) newString += "0";
return newString;
}

Wednesday, July 08, 2009

Maintain scroll position for Listbox ASP.NET

I was facing a problem with maintaining scroll position for a list box inside a update panel in ASP.NET.
So I tried o Google the problem and found no convincing answers.
Finally I got it working by a small JavaScript method that was called at end request handler for any AJAX calls for the page.
The JavaScript is as follows:
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function EndRequestHandler(sender, args) {
// reseting the all selected indices again for multi selection or single selection in the reverse order
// inorder to get the scroll postion at the top of first selection
var lstBoxLen = document.getElementById(lstEmployees).length;
for (var i = lstBoxLen - 1; i >= 0; i--) {
if (document.getElementById(lstEmployees).options[i].selected) {
document.getElementById(lstEmployees).options[i].selected = true;
}
}

}
This solution is now working for both single selection and multi selection based list box.
I would appreciate any other better way of doing this.

May be this will be helpful to some one who is finding it difficult to restore scroll position of list box after auto post back.