Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert decimal? to decimal

may be it is a simple question but I'm try all of conversion method! and it still has error! would you help me?

decimal? (nullable decimal) to decimal

like image 780
Negar Avatar asked Feb 02 '11 07:02

Negar


People also ask

How do you convert a decimal number to a decimal?

Step 1 − Divide the decimal number to be converted by the value of the new base. Step 2 − Get the remainder from Step 1 as the rightmost digit (least significant digit) of new base number. Step 3 − Divide the quotient of the previous divide by the new base.

What are the rules to convert decimal?

An easy method of converting decimal to binary number equivalents is to write down the decimal number and to continually divide-by-2 (two) to give a result and a remainder of either a “1” or a “0” until the final result equals zero. So for example. Convert the decimal number 29410 into its binary number equivalent.


2 Answers

There's plenty of options...

decimal? x = ...  decimal a = (decimal)x; // works; throws if x was null decimal b = x ?? 123M; // works; defaults to 123M if x was null decimal c = x.Value; // works; throws if x was null decimal d = x.GetValueOrDefault(); // works; defaults to 0M if x was null decimal e = x.GetValueOrDefault(123M); // works; defaults to 123M if x was null object o = x; // this is not the ideal usage! decimal f = (decimal)o; // works; throws if x was null; boxes otherwise 
like image 54
Marc Gravell Avatar answered Oct 08 '22 21:10

Marc Gravell


Try using the ?? operator:

decimal? value=12; decimal value2=value??0; 

0 is the value you want when the decimal? is null.

like image 40
Carles Company Avatar answered Oct 08 '22 22:10

Carles Company