简体   繁体   中英

entity framework decimal scale

I'm using Entity Framework 6.0.2, database first approach. Currently in my database I have a field which is set to decimal(5,2). When the entity framework model is generated I see the properties of the model also show the precision set to 5 and scale to 2 and type to decimal. The generated class has it built as:

public Nullable<decimal> ContractorAmount { get; set; }

Whenever I try to read a value from it which has a ending 0, such as 233.10, it comes back from the context as 233.1. the ending 0 missing, is there a way to correct this? it works fine when the ending value is not 0.

I think that you are confusing storage format and presentation format .

decimal(5,2) will guarantee that you have a decimal number with two decimals that will be exact.

When converting the data to string for displaying by default it omits any trailing decimal 0. If you want them written out all the time, you should use a formatting specifier to ensure that is done.

Decimal d = 0.1M;
Console.WriteLine(d);
Console.WriteLine(d.ToString("0.00"));

The first line will output 0.1 , the second will output 0.10 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM