1

我正在创建一个模型,我希望所有的十进制值最终都是 2 位小数(货币)。这是我的想法,但显然行不通。

public class Fees
{
    public decimal permitFee { set; get { return Math.Round(permitFee, 2); } }
    public decimal planReviewFee { set; get { return Math.Round(planReviewFee, 2); } }
    public decimal surchargeFee { set; get { return Math.Round(surchargeFee, 2); } }
    public decimal totalFee { get { return permitFee + planReviewFee + surchargeFee; } }
}

我应该如何设置我的对象以便获得正确的输出值?

4

3 回答 3

4

我不相信你可以使用这样的属性(返回自身)。尝试对属性使用基础变量,看看是否能解决您的问题。

C# set/get 中的堆栈溢出错误

于 2012-05-16T17:09:25.313 回答
1

请参阅我上面的评论并考虑将值存储为整数

public class Fees {  

private int permit;  
private int planReview;  
private int surcharge;  
public decimal permitFee { set { permit = (int) (value * 100); }; 
                           get { return permit / 100; } }  
public decimal planReviewFee { set { planReview = (int) (value * 100); } 
                               get { return planReview / 100; } }
public decimal surchargeFee { set { surcharge = (int) (value * 100); } 
                              get { return surcharge / 100; } }
public decimal totalFee { get { return permitFee + planReviewFee + surchargeFee; } }  }  }
于 2012-05-16T17:17:34.713 回答
0

这是我最终接受@tom_yes_tom 的建议的结果。

public class Fees
{
    private decimal permit;
    private decimal planReview;
    private decimal surcharge;

    public decimal permitFee
    {
        set { permit = Math.Round(value, 2); }
        get { return permit; }
    }
    public decimal planReviewFee 
    {
        set { planReview = Math.Round(value, 2); }
        get { return planReview; } 
    }
    public decimal surchargeFee 
    {
        set { surcharge = Math.Round(value, 2); }
        get { return surcharge; } 
    }
    public decimal totalFee 
    { 
        get { return permitFee + planReviewFee + surchargeFee; } 
    }
}
于 2012-05-16T18:18:04.360 回答