Friday, February 6, 2009

Rounding to the nearest 1000 in C#

Working on that demo app, this came up - I wanted a number rounded to the nearest 1000. This is easy enough in Excel - the ROUND function takes negative values, so that =ROUND(2009,-3) will give you 2000. The Math.Round function in System.dll doesn't offer this functionality however - you have to implement that yourself. Here's an example of one way to do this:

using System;
 
static class Program
{
    static void Main(string[] args)
    {
        double d = (double)new Random().Next();
        // round to the nearest 1000
        Console.WriteLine("{0} rounded to the nearest thousand is {1}", d, Round(d, -3));
        // round to the nearest 100 etc
        Console.WriteLine("{0} rounded to the nearest hundred is {1}", d, Round(d, -2));
    }
 
    static double Round(double value, int digits)
    {
        if ((digits < -15) || (digits > 15))
            throw new ArgumentOutOfRangeException("digits", "Rounding digits must be between -15 and 15, inclusive.");
 
        if (digits >= 0)
            return Math.Round(value, digits);
 
        double n = Math.Pow(10, -digits);
        return Math.Round(value / n, 0) * n;
    }
 
    static decimal Round(decimal d, int decimals)
    {
        if ((decimals < -28) || (decimals > 28))
            throw new ArgumentOutOfRangeException("decimals", "Rounding decimals must be between -28 and 28, inclusive.");
 
        if (decimals >= 0)
            return decimal.Round(d, decimals);
 
        decimal n = (decimal)Math.Pow(10, -decimals);
        return decimal.Round(d / n, 0) * n;
    }
}

SharePoint API: A Client-side Object Model - Demo Application

Back in November we released HubKey's Client-side API for SharePoint. This API wraps many of SharePoint Server 2007's Web Services (Lists Web Service, Webs Web Service, etc.) to provide a familiar object model that can be used on remote client machines without having to process XML requests and responses.

We've recently made a few updates and have included a demonstration application which is shown running in the screen capture below. This demo app creates a new list on a remote test site with a quick launch link, creates a "test person" list scoped content type, adds a number of randomly generated people records, and then adds a new default view with a custom query returning people below age 65. This list data is then browsable and editable by a DataGridView hosted in a Windows form. In addition, paging is demonstrated by using the SPListItemCollectionPosition object.

You can download a demo copy of the API - click here for details.

MS Dynamics CRM 4.0 Record Counter / Page Count Addon

One feature commonly requested by users of Microsoft Dynamics CRM is a total of the number of records in a view or advanced find.

Out-of-the-box, MS CRM 4.0 displays both a count of the number of records selected on the page, and the total number of records on the page, both of which are visible in the status bar at the bottom of the results grid for the view. A count of the number of records in total is missing however, as is the total number of pages available.

HubKey's new Record Count Add-on for Microsoft Dynamics CRM 4.0 meets this feature request by including both the current page number, the total pages, and the total records in the status bar along side the record selection count.

Record count in status bar

This add-on is simple to install, can be configured to exclude certain entities, and supports configurable multiple user interface languages. The totals are calculated for quick and advanced finds, lookup views, and both private and public views.

A demonstration of the installation and use of the add-in is shown in the screen capture below:



To download a free trial version of the software and get a 30 day license key, click here.

To purchase a full retail server license key for $350 (per CRM organization), please use HubKey's application web store on this page or contact us at HubKey for more information.