C# 写入x=x*(1+;标记)的快捷方式
我们正在编写一个处理产品定价的应用程序。正如您可能想象的那样,相当于C# 写入x=x*(1+;标记)的快捷方式,c#,.net,C#,.net,我们正在编写一个处理产品定价的应用程序。正如您可能想象的那样,相当于Price=Price*(1+MarkupPercent)的东西有很多用途贯穿整个代码 我去寻找一个操作符,可能类似于+=,它不仅能让写东西更容易,还能让读东西更容易。不走运,至少据我所知。由于Price和MarkupPercent都是float类型,我开始考虑扩展float以添加标记(浮动百分比) 这似乎是可行的,但我宁愿使用更标准的方式(即包含在框架中)也不愿开始对基类进行随机扩展。建议? 鉴于以上所有的评论都回答了这个问题
Price=Price*(1+MarkupPercent)的东西有很多用途代码>贯穿整个代码
我去寻找一个操作符,可能类似于+=
,它不仅能让写东西更容易,还能让读东西更容易。不走运,至少据我所知。由于Price
和MarkupPercent
都是float
类型,我开始考虑扩展float
以添加标记(浮动百分比)
这似乎是可行的,但我宁愿使用更标准的方式(即包含在框架中)也不愿开始对基类进行随机扩展。建议?
鉴于以上所有的评论都回答了这个问题,而且没有实际的答案,我写这篇文章
解决这一问题的最著名有效方法(基本上)如下:
public Decimal Price { get; set; } // shorthanded here for brevity
public Decimal GetMarkedUpPrice(Decimal Percent)
{
return Price * (1 + Percent);
}
好的,“最有名”可能是双曲线。不过,这段代码捕获了各种注释的本质。如果我抓住了这个错误,请随时纠正我
或者,只需适当加价即可:
public Decimal Price { get; set; } // shorthanded here for brevity
public void MarkupThePrice(Decimal Percent)
{
Price *= (1 + Percent);
}
以下是一些备选方案,为您提供了使用该类的不同方式:
public class Price
{
// Instance properties
public decimal BasePrice { get; set; }
public decimal PercentMarkup { get; set; }
public decimal MarkedupPrice { get { return GetMarkedUpPrice(PercentMarkup); } }
// Instance method to allow you to show additional markup
// prices without changing the instance property
public decimal GetMarkedUpPrice(decimal percentMarkup)
{
return GetMarkedUpPrice(BasePrice, percentMarkup);
}
// Static method to get a quick calculation without instance overhead
public static decimal GetMarkedUpPrice(decimal basePrice, decimal percentMarkup)
{
return basePrice * (1 + percentMarkup);
}
}
示例用法:
public static void Main()
{
var hammerPrice = new Price {BasePrice = 10, PercentMarkup = .15m};
Console.WriteLine("{0:C} hammers will be sold for {1:C}, except on " +
"Sunday, when they will be {2:C}", hammerPrice.BasePrice,
hammerPrice.MarkedupPrice, hammerPrice.GetMarkedUpPrice(.1m));
Console.WriteLine("Otherwise, items with a base price of $10.00 " +
"will be sold for {0:C}", Price.GetMarkedUpPrice(10, .2m));
}
public static void Main()
{
decimal somePrice = 5.6m;
decimal markedUpPrice = somePrice.GetMarkedUpPrice(.5m);
}
另一种方法是为decimal
类型编写扩展方法,这将为您提供一些附加功能:
public static class Extensions
{
public static decimal GetMarkedUpPrice(this decimal basePrice, decimal percentMarkup)
{
return basePrice * (1 + percentMarkup);
}
}
用法示例:
public static void Main()
{
var hammerPrice = new Price {BasePrice = 10, PercentMarkup = .15m};
Console.WriteLine("{0:C} hammers will be sold for {1:C}, except on " +
"Sunday, when they will be {2:C}", hammerPrice.BasePrice,
hammerPrice.MarkedupPrice, hammerPrice.GetMarkedUpPrice(.1m));
Console.WriteLine("Otherwise, items with a base price of $10.00 " +
"will be sold for {0:C}", Price.GetMarkedUpPrice(10, .2m));
}
public static void Main()
{
decimal somePrice = 5.6m;
decimal markedUpPrice = somePrice.GetMarkedUpPrice(.5m);
}
您可以使用*=
:价格*=(1+MarkupPercent)
@caleb看起来像是我的答案:)旁注:我希望您知道float
数据类型的精度有限。通常它从不用于重复货币值。十进制。。另外,如果您反复使用一个代码,那么创建一个只指定了getter的属性不是更好吗?还是一种方法,不管什么都有意义?这样看来,它似乎违反了干式原理。…。@J.D.Ray:float的精度只给出了六个有效数字。一个double
可以提供更好的精度,但由于它是一个浮点数,所以仍然可能会出现舍入错误。通常,decimal
用于货币值,因为它精确地表示一个数字。我会在1+百分比
周围加上括号。它使我们更清楚地知道目的是什么。