Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why can't you assign a number with a decimal point to decimal type directly without using type suffix?

Tags:

c#

Why can't you assign a number with a decimal point to the decimal type directly without using type suffix? isn't this kind of number considered a number of type decimal?

decimal bankBalance = 3433.20; // ERROR! 
like image 930
prodev42 Avatar asked Jan 27 '09 20:01

prodev42


People also ask

What happens if a number is assigned with a decimal point to an integer rather than to a float?

The conversion truncates; that is, the fractional part is discarded.

How does a number with a decimal point differ from a number without one?

Decimals are a shorthand way to write fractions and mixed numbers with denominators that are powers of 10 , like 10,100,1000,10000, etc. If a number has a decimal point , then the first digit to the right of the decimal point indicates the number of tenths. For example, the decimal 0.3 is the same as the fraction 310 .

Why Cannot insert decimal in Excel?

Click the File tab. Click on Options. In the Excel Options dialog box that opens up, click on the 'Advanced' option in the left pane. In the editing options, enable the setting – “Automatically insert or decimal point”

Why can't you say decimal as an integer?

Answer. An integer, also called a "round number" or “whole number,” is any positive or negative number that does not include decimal parts or fractions. For example, 3, -10, and 1,025 are all integers, but 2.76 (decimal), 1.5 (decimal), and 3 ½ (fraction) are not.


2 Answers

Edit: I may have missed the last part of the question, so the overview below is hardly useful.

Anyway, the reason you can't do what you're trying to do is because there is no implicit conversion between floating point types and decimal. You can however assign it from an integer, as there is an implicit conversion from int to decimal.


You can, but you have to use this syntax (or do an explicit cast to decimal).

decimal bankBalance = 3433.20m; 

and for floats it is

float bankBalance = 3433.20f; 

default is double

double bankBalance = 3444.20; 
like image 191
Brian Rasmussen Avatar answered Sep 25 '22 02:09

Brian Rasmussen


Actually, hidden spec feature: you can ;-p

decimal bankBalance = (decimal)3433.20; 

This is genuinely parsed by the compiler as a decimal (not a float and a cast). See the IL to prove it. Note that the precision gets truncated, though (this has 1 decimal digit, not the 2 you get from the M version).

IL generated:

L_0001: ldc.i4 0x861c L_0006: ldc.i4.0  L_0007: ldc.i4.0  L_0008: ldc.i4.0  L_0009: ldc.i4.1  L_000a: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8) L_000f: stloc.0  

Compared to:

decimal bankBalance = 3433.20M; 

Which generates:

L_0001: ldc.i4 0x53d18 L_0006: ldc.i4.0  L_0007: ldc.i4.0  L_0008: ldc.i4.0  L_0009: ldc.i4.2  L_000a: newobj instance void [mscorlib]System.Decimal::.ctor(int32, int32, int32, bool, uint8) L_000f: stloc.0  

The only difference is the decimal digits (1 vs 2, and a factor of 10, accordingly)

like image 37
Marc Gravell Avatar answered Sep 24 '22 02:09

Marc Gravell