- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个小网站,并且有 20 种产品销售。
每周我都想计算每种产品的新价格,考虑每种产品的销售数量,但遵循两个规则:
1 - 20 种产品的价格总和必须始终为 100 欧元
2 - 我希望每个产品都是整数(无小数)或 0.5,我的意思是,一种产品的价格可以是 7 或 8 或 9 或 7.5 或 8.5,但不能是 8.3 或 7.1。
3- 产品的最低价格为 1.5。
让我们举个例子,但用 4 种产品进行简化:
本周售出:
product_id sells
---------- ------
1 45
2 31
3 12
4 62
好的,总销量是 150。
如果我将这 100 欧元除以 150 次销售,我会得到 0.667 欧元/销售。因此,如果我将每个产品乘以这个值,我会得到:
product_id sells raw_price_next_week
---------- ------ -------------------
1 45 30.015
2 31 20.677
3 12 8.004
4 62 41.354
然后我需要考虑到我之前提到的第二条规则,对这些 raw_prices 进行标准化,获取每个 raw_value 最接近的 0.5 倍...
product_id sells raw_price_next_week final_price
---------- ------ ------------------- ------------
1 45 30.015 30
2 31 20.677 20.5
3 12 8.004 8
4 62 41.354 41.5
好吧,我有一个代码,除了第三点(最低价格为 1.5 的那一点)之外,一切都可以。因此,例如,如果产品的原始价格为 0.5,则价格应为 1.5,因为这是最低价格。
我想,那么首先要做的应该是计算当原始价格低于 1.5 时调整到最低 1.5 欧元的产品花费了多少欧元,并将调整后价格的总和存储在 var 中(想象一下 6 欧元)。之后,100 欧元 - 6 欧元 = 94 欧元。用这个数量运行算法的其余部分......
这是到目前为止的代码:
// Start with the raw product data (array of object - from database likely)
var
products = [
{
product_id: 1,
sells: 45
},
{
product_id: 2,
sells: 31
},
{
product_id: 3,
sells: 12
},
{
product_id: 4,
sells: 62
},
{
product_id: 5,
sells: 1
},
{
product_id: 6,
sells: 2
}
]
;
// ###############################################################
window.Pricing = {
total_sum: 100, // This represents the 100€
sales : 0,
min_price : 1.5
init: function( products ) {
return this.calculate_final_price( products );
},
get_price_per_sale: function( products ) {
for( x in products ) {
this.sales += products[x].sells;
}
return this.total_sum / this.sales;
},
calculate_final_price: function( products ) {
var price_per_sale = this.get_price_per_sale( products );
for( x in products ) {
products[x].raw_price_next_week = +( products[x].sells * price_per_sale );
products[x].final_price = +( Math.round( products[x].raw_price_next_week * 2 ) / 2 ).toFixed(1);
}
return products;
}
};
// ###############################################################
// Init the pricing calculations and get the final objects with updated pricing.
Pricing.init( products );
最佳答案
我添加了一些变量进行验证,然后让函数自行调用,直到得出有效的解决方案。
// Start with the raw product data (array of object - from database
var products = [ { product_id: 1, sells: 45 }, { product_id: 2, sells: 31 }, { product_id: 3, sells: 12 }, { product_id: 4, sells: 62 }, { product_id: 5, sells: 1 }, { product_id: 6, sells: 2 } ] ;
var Pricing = {
necessary_sum: 100,
dummy_sum: 100, // This represents the 100€
sales : 0,
min_price : 1.5,
real_sum : 0,
init: function( products ) {
return this.calculate_final_price( products );
},
get_price_per_sale: function( products ) {
this.sales = 0;
for( x in products ) {
this.sales += products[x].sells;
}
return this.dummy_sum / this.sales;
},
calculate_final_price: function( products ) {
var price_per_sale = this.get_price_per_sale( products );
for( x in products ) {
products[x].raw_price_next_week = products[x].sells * price_per_sale;
products[x].final_price = (Math.round( products[x].raw_price_next_week * 2 ) / 2 ).toFixed(1);
if(products[x].final_price < this.min_price){
products[x].final_price = this.min_price;
}
this.real_sum += ~~products[x].final_price;
}
if(this.real_sum != this.necessary_sum){
this.dummy_sum -= this.real_sum - this.necessary_sum;
this.real_sum = 0;
price_per_sale = this.get_price_per_sale( products );
this.calculate_final_price(products);
}
return products;
}
};
// Init the pricing calculations and get the final objects with updated pricing.
console.log(Pricing.init( products ));
console.log('should be: ' + Pricing.necessary_sum);
console.log('really: ' + Pricing.real_sum);
关于用于计算产品价格的 JavaScript 构建和算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28973914/
我需要一些帮助。 我希望“总计”由“数量*价格=总计”计算(到目前为止没问题)。问题是我还需要通过“总/价格=数量”来计算“数量”,即如果一个字段发生更改,另一个字段也会自动更改。 我做了一个非常简单
我试图将每件商品的数量和价格相乘来计算总数,但我的警报中出现错误。 $.each(data.items, function(index, d){ var calcultest = d.pric
我想获得格式化的价格但没有货币符号,我只想使用 magento 的标准功能! $product->getFinalPrice(); => 19.9900 Mage::helper('core')->f
我正在尝试获取特定月份和年份中所有父类别的总价格。父类别是 parent_id == 0 的任何类别。我的查询如下所示: SELECT ROUND(SUM(od.total_price)) a
请帮我摆脱我的头痛..提前我为我的糟糕语言道歉,无论是英语还是mysql。希望有人能理解这个问题..:) 我有一个数据库,任何人都可以记录各个商店中各种产品的价格。以下查询是一个半理论示例,可能根本不
下面是我需要在数据库中设计的示例: 会有一个价格选项,如果有的话,会有一个特价选项,然后我想知道如果我希望其中一个选项是“免费”的,我该怎么做。 另请参阅根据所在国家/地区会有不同的货币。这是我的想法
商品价格格式 999,99 999 - 1 ..4 digits , - comma sign marks decimal point 99 - 2 digits after price Postg
我有这个表 stk +---------+--------------+ | Field | Type | +---------+--------------+ | id
是否有一个简单的格式化程序可以将我的字符串格式化为价格? 所以我的字符串是:300000 我想用空格来“300 000” or 1000000 "1 000 000" 张国荣 最佳答案 这样做: St
我想知道是否可以使用不依赖于 Excel 应用程序本地化(欧盟/美国)的 Excel 公式来自定义数字格式? 例如,我的值为 1291660。 然后使用公式=TEXT(A1;"# ##0,00") .
这是我的代码,对于价格 slider : $("#price-slider").ionRangeSlider({ min: 130, max: 575, onChange :
用户可以使用价格创建一个新实体。价格可以使用不同的货币(EUR,USD ...),因此我们可以乘以(price * convert_rate)得到实际价格。 我想做的是根据价格过滤记录,具体取决于前端
我正在尝试隐藏小数位在类型输入字段上,例如 数字从0.00开始 因此,输入字段中的第一个位置将为 0.00 我输入的1比它应该变成0.01 我输入的2比它应该变成0.12 比 0 所以它应该变成 1.
$res=mysql_query($qry); while($row= mysql_fetch_array($res)) { echo "".$row['Food_Name']." ".$row['P
我们正在为我们的新项目寻找信用卡网关。那里一片困惑,所有人都想把你切成碎片。每次我与他们交谈时,他们都有不同的费率,每次更新报价时,他们都会更改一些价格。 我们正在使用 .net、C#、asp.net
我已经创建了一个 jQuery 价格 slider ,但我不确定如何让过滤区域以实际价格范围开始?目前它有“$[object Object] - $[object Object]”,而我希望它有“$2
我已经创建了 jquery 价格 slider ,但我不确定如何过滤我的结果,以便在滑动时您只能看到具有该值范围内的产品。 HTML: Price range:
我有一个页面,其中有一个表格,我们可以在其中选择一个产品,输入它的数量和价格,然后应在最后一列中计算金额,并应添加新行以输入更多产品,直到我们完成为止。
我创建了电子商务网站,即将提供午餐和晚餐服务。我在这里提出问题/问题是因为我知道这里有很多可以帮助我的传奇人物。我在网站上添加了套餐/计划部分。 1. Weekly 2. Monthly 以下是订
我的网站需要一个简单的 jQuery 价格 slider 。从 0 英镑到 1000 英镑不等。 假设浏览器将 slider 设置为 100 英镑(例如),然后我需要一个立即购买按钮,该按钮将 sli
我是一名优秀的程序员,十分优秀!