Tag Archive for 'convertion'

C#, decimal.toString(), and how to get rid of trailing zeros

Today I spend A LOT OF time on trying to display a percentage – string representation of a decimal but without trailing zeros, e.g. 15.1% instead of 15.10%.

In the application I already had NumberFormatInfo for displaying monetary values and percentages. An object of that type was defined as follows:

NumberFormatInfo nfi = new NumberFormatInfo();
nfi.PercentDecimalDigits = 2;
// some additional settings for nfi 
// (including monetary and numeric attributes)

Continue reading ‘C#, decimal.toString(), and how to get rid of trailing zeros’

Media convertion

Many times it happened to me that I needed to convert a file from one format to another (text documents, images, movies, etc). It can be difficult and expensive to download different pieces of software that can do that.

Media Convert service comes with helping hand. Let me cite the authors:

Media-Convert is 100% free. No software is needed, and you don’t have to register. You only need your favorite Internet browser. Your files are ready 7/7 days 24/24 hours (…).

Why use Media-Convert?

Simply because Media-Convert is the best manner of converting audio files, video, presentation, document, spreadsheet, etc.

Media-Convert advantages:

  • More secure because you do not have to install software on your computer, which can contain viruses, spywares, etc.
  • Access from everywhere in the world through Internet. You can use Media-Convert at the office, in a cybercafé, or with the PC of a friend, and then download the result quietly to your mp3 reader, cell phone or any other hardware.

So far, I must admit I’m happy with it. However, I’m not guy who converts things everyday icon wink Media convertion

Besided that service, I’ve encountered another, MC. I didn’t use it much, though. I tried to test it with convertion of doc files to pdf but all tries ended up with “You used an unknown file type!” message. It discouraged me from discovering what I did wrong, if I could do such at all icon wink Media convertion

SQL: CONVERT to DATETIME

I’ve heard that SQL Server developers happen to have problems with CONVERT function when try to obtain a value of datetime type. Recently, I’ve had that problem during a Data Transformation Services (DTS) package implementation.

One of the task in that package was “Transform Data”, which transferred data between a source and destination. To retrieve that data, I used SQL query within which I needed to add a number of milliseconds to a given date. The following snippet shows what I really wanted to execute:

dateadd (millisecond, ?, convert(datetime,?))

In abovementioned snippet, both question marks (?) are different parameters used in that query. The former is sINTEGER (the number of millseconds) whereas the latter is the date kept as VARCHAR. The latter parameter needs convertion to DATETIME.

Unfortunately, I could’t achieve what I really wanted. When I put the abovemeantioned snippet in the query in “Transform Data Task Properties” window, I couldn’t even save it – got the following error: “… argument data type datetime is invalid for argument 2 of dateadd function”.

I spent some time analyzing what was going on. I ran thas query in Query Analyzer with hardcoded values and it was ok. Finally, I used Profiler tool to see what exactly happened when the task (and consequently that query) was run. That gave me the explanation: the second parameter was unnecessary converted by the engine which executed that package. After that convertion, the value of that parameter was wrong and finally was converted second time to a wrong DATE value.

The solution to that problem is quite easy: the described query can be put in a stored procedure which is executed during the transformation. The SQL engine had difficulties defining the data types of the parameters. Luckily, in a stored procedure, the data types are explicitly defined.