Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

c# - EntityFramework pence to pounds

I'm building a WPF application. I made my database using Code-First.

I need to insert prices of items in pence (e.g. 500), but when prices will be displayed they should be in pounds (5,00).

I tried to do the "conversion" in the setter of my property

public decimal Price
{
   get { return price; }
   set { price = value/100 ;}
}

But, for some reason, when I drop manually the database, the values in my ListView are shown some times like 0,05 and other times like 500,00. Something must be wrong with this method.

How can I tell my database to consider last 2 numbers of my value as decimal?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You might want to try something like this

public class YourClassName {
   public decimal Price { get; set; }

   [NotMapped]
   public decimal Pounds => Price/100;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...