.NET

Formatting scaled numbers

Standard formatting in .NET is sometimes not enough to fill specific requirements. For example, when high-level financial reports from big corporations need to be produced, the numbers dealt-with are generally around hundred of millions. Consider the following report where the scale and the currency (M$) is being displayed in the row header.

Quarter 1Quarter 2
New York (in M$)102.7621.40
Florida (in M$)32.7667.40

In order to achieve this formatting, a straightforward solution would be to apply a scaling factor to the numbers beforehand.

string.Format("{0:N2}", valueToFormat / 1000000); 

Sometimes, it is not appropriate nor possible to scale the numbers. In this situation, number scaling using the "," custom specifier could be used. Note the use of two commas since the number to be formatted is divided by 1000 for each comma:

decimal valueToFormat = 1345278000.346M;
string.Format("0:#,#,,", valueToFormat ); // displays 1,345 with the invariant culture

Order returned by GetMethods

When calling the GetMethods function which lists the methods for a given type, we should keep in mind that this function does not return methods in a particular order. More specifically, the methods are not necessarily in the same order it has been declared in the source code. This discrepancy is due to some caching performed by the .NET Framework for better performance as explained in a post by Haibo Luo. This article is aimed at outlining solutions to order the methods returned by GetMethods in the declaring order.

Context Connection and SQLCLR Functions

For one of the projects I have worked on recently, a set of variables needed to be computed for each product and for each month of the year on a regular basis. The product data was stored in SQL Server 2005 SP2. The computed volume was quite large since around 60 variables x 120 products x 12 months = 86,400 results needed to be calculated every time and be stored in the result table. The business requirements were the following:

  • The calculated variables were based on complex business rules which were quite tricky to be implemented in TSQL.
  • Each variable related to a product had to use input from various tables.
  • Variables related to a given product were independent of other products.
  • On the other hand, variables were sometimes based on others variables for the same product.

It was decided to express the business rules using a .NET language such as C# and to create a Table-Valued Function (TVF) using the CLR. Srinivas Sampath wrote a good introduction on how to implement a TVF in .NET.

Testing Equality Operators with NUnit

Any class or structure in .NET can define the equality and inequality operators. Implementing such operators, however, can be error-prone. The operators therefore need to be validated by unit tests. We will first discuss the challenges of writing such unit tests.

QueryBuilder

Unpublished

Pulling data from the database often requires to write complex SQL statements. Similar statements are often constructed by data access objects. The use of stored procedures deployed on the database server is sometimes the solution to factorize and centralized these SQL statements. However, with complex business rules changing quite often, generating SQL statements through code is often the most simple and fatest solution available. Let's take an example of this following DAO pulling the list of the customers:

Software FX

Position: 
.NET Architect
Employment Date: 
September, 2004 to November, 2006

ChartFX logo Software FX is a component vendor and one of the leaders in graph solutions. Their flagship product is Chart FX.

Geography: 
Subscribe to RSS - .NET