0

Simple question, but i'm stucked...

I want to set the plusmin on initialisation dependant on the defaultQty.

So is the defaultQty = 1 (of an other whole number like 2 or 3) then the plusmin has to be 1.

If it's not a whole integer (like 0.5) the plusmin has to be 0.1

define([
    'ko',
    'uiComponent'
], function (ko, Component) {
    'use strict';

    return Component.extend({
        initialize: function () {
            //initialize parent Component
            this._super();
            this.qty = ko.observable(this.defaultQty);
            this.plusmin = 0.1;
        },

        decreaseQty: function() {
            var newQty = this.qty() - this.plusmin;
            if (newQty < this.defaultQty) {
                newQty = this.defaultQty;
            }
            this.qty(newQty);
        },

        increaseQty: function() {
            var newQty = this.qty() + this.plusmin;
            this.qty(newQty);
        }

    });
});
1
  • this.plusmin = ( this.qty == Math.floor(this.qty) ) ? 1 : 0.1 Commented Nov 14, 2016 at 14:58

2 Answers 2

3

this.plusmin = (this.defaultQty % 1 === 0) ? 1 : 0.1;

Sign up to request clarification or add additional context in comments.

3 Comments

Yep! This is it! in 4 minutes i can accept the answer. But when i go to 0.8 in some way 0,7999999999999999 appears in the input field. Is there a way to (conditional) round the number to 0,8 when it is not a whole number?
Not sure exactly what you need here. JavaScript gets funny with number precision in ways such as the above, but it shouldn't affect the answer to this question. If that's an entirely new issue, then just ask it as a new question.
Used var newQty = Math.round((this.qty() - this.plusmin) * 100) / 100; to do the job :-)
0

You can use the modulus operator (%) which returns the remainder after dividing:

(defaultQty % 1) > 0 ? plusmin = 0.1 : plusmin = 1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.