Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert numeric types: Which style to use?

Tags:

c#

.net

vb.net

Let's say I want to convert a Double x to a Decimal y. There's a lot of ways to do that:

1. var y = Convert.ToDecimal(x);    // Dim y = Convert.ToDecimal(x)
2. var y = new Decimal(x);          // Dim y = new Decimal(x)
3. var y = (decimal)x;              // Dim y = CType(x, Decimal)
4. -- no C# equivalent --           // Dim y = CDec(x)

Functionally, all of the above do the same thing (as far as I can tell). Other than personal taste and style, is there a particular reason to choose one option over the other?

EDIT: This is the IL generated by compiling the three C# options in a Release configuration:

1. call valuetype [mscorlib]System.Decimal [mscorlib]System.Convert::ToDecimal(float64)
   --> which calls System.Decimal::op_Explicit(float64)
       --> which calls System.Decimal::.ctor(float64)
2. newobj instance void [mscorlib]System.Decimal::.ctor(float64)
3. call valuetype [mscorlib]System.Decimal [mscorlib]System.Decimal::op_Explicit(float64)
   --> which calls System.Decimal::.ctor(float64)

This is the IL generated by compiling the four VB options in a Release configuration:

1. call valuetype [mscorlib]System.Decimal [mscorlib]System.Convert::ToDecimal(float64)
   --> which calls System.Decimal::op_Explicit(float64)
       --> which calls System.Decimal::.ctor(float64)
2. call instance void [mscorlib]System.Decimal::.ctor(float64)  
3. newobj instance void [mscorlib]System.Decimal::.ctor(float64)
4. newobj instance void [mscorlib]System.Decimal::.ctor(float64)

So, it all ends up in System.Decimal::.ctor(float64)

like image 719
Heinzi Avatar asked Oct 05 '11 13:10

Heinzi


People also ask

Which method can be used to convert a numeric value to a string?

toString() Method. Integer class provides a method toString () to convert int to String. This method converts the value to the signed decimal representation and returns a String object. Let's understand the use of this Integer(int).

Which is better cast or convert in SQL?

CONVERT is SQL Server specific, CAST is ANSI. CONVERT is more flexible in that you can format dates etc. Other than that, they are pretty much the same. If you don't care about the extended features, use CAST .

How do we convert the different data types?

Data types can be converted either implicitly or explicitly. Implicit conversions are not visible to the user. SQL Server automatically converts the data from one data type to another. For example, when a smallint is compared to an int, the smallint is implicitly converted to int before the comparison proceeds.

Which data type is used for numbers?

Numeric Data Types An exact numeric value has a precision and a scale. The precision is a positive integer that determines the number of significant digits in a particular radix. The data types NUMERIC, DECIMAL, INTEGER, BIGINT, and SMALLINT are exact numeric types.


3 Answers

I would suspect that using the unary casting operator would give the compiler a better chance to optimize, but I base this on absolutely nothing.

like image 56
GeirGrusom Avatar answered Sep 23 '22 05:09

GeirGrusom


Convert.ToInt32() applies rounding to real numbers while casting to int just removes the fractional part. In my opinion typecasting method for "conversions" relies on .NET framework's magic too much. If you know that a conversion will have to take place, describing it explicitly is the easiest to understand. I would go for Convert option for most of the cases unless there is no conversion needed and a cast just works.

like image 41
Sedat Kapanoglu Avatar answered Sep 22 '22 05:09

Sedat Kapanoglu


With non-object and non-string source data types, I prefer casts. Concise and easy to read.

For object and string source types, it depends on the source. For example, if the source is from the user, then I use Convert/Conversion methods.

I recommend downloading the source code for .NET at "Microsoft Reference Source Code Center" (http://referencesource.microsoft.com). Check out Convert.cs, Converstion.vb, and Conversions.vb. I previously would use the Convert/Conversion methods in all cases, but after reviewing these source files, my preference is casts.

Taking your example Convert.ToDecimal() and referencing Convert.cs, the call is really a simple cast:

    public static decimal ToDecimal(double value) { 
        return (decimal)value;
    }
like image 27
AMissico Avatar answered Sep 20 '22 05:09

AMissico