任何人都可以帮我一件事。
我有某种在线商店,当用户放置他的商品时,它会出现两个字段。
第一个是价格(the one that user will receive when item will be sold
),第二个是 buyPrice (the one that another user will pay for this item
),它们之间的差异是费用( equal to 0.08
)。
当buyPrice小于或等于0.07
时,我遇到问题,因为价格不会改变。我用来修复函数来处理浮点数。
这是js代码:
@baseFee = window.app.fee
@fee = (val) ->
if +val <= 0.07 then 0.6 else @baseFee
@price = ko.observable('').extend({ throttle: 150 })
@buyPrice = ko.observable('').extend({ throttle: 150 })
@price.subscribe =>
price = +@price()
@updateBuyPrice price if price > 0
@buyPrice.subscribe =>
buyPrice = +@buyPrice()
@updatePrice buyPrice if buyPrice > 0
@updatePrice = (buyPrice) =>
price = +@price()
newPrice = (buyPrice - buyPrice * @fee(buyPrice)).toFixed(2)
newBuyPrice = (price + price * @fee(buyPrice)).toFixed(2)
@price newPrice if buyPrice != +newBuyPrice
@updateBuyPrice = (price) =>
buyPrice = +@buyPrice()
newPrice = (buyPrice - buyPrice * @fee(price)).toFixed(2)
newBuyPrice = (price + price * @fee(price)).toFixed(2)
@buyPrice newBuyPrice if price != +newPrice
这里是我应用此内容的HTML:
.form-group.pull-left
label for="price" You will get:
div data-bind="css: selectedItem().priceCssClass()"
.input-group-addon= Settings[:app][:currency][:sign]
input.form-control type="number" step="0.01" id="price" data-bind="value: selectedItem().price, valueUpdate: 'afterkeydown'"
.form-group.pull-left
label for="buy-price" Buyer will pay:
div data-bind="css: selectedItem().buyPriceCssClass()"
.input-group-addon= Settings[:app][:currency][:sign]
input.form-control type="number" step="0.01" id="buy-price" data-bind="value: selectedItem().buyPrice, valueUpdate: '"
当我选择buyPrice作为0.08
时,这个逻辑做了一些额外的循环,因为价格将等于0.08 - 0.08 * 0.08 = 0.07
,因此费用值将从0.08
更改为{ {1}}由于@fee函数。
我不知道如何解决这个问题,或者我应该改变逻辑。
所以我会很乐意为你提供帮助。
谢谢!