【问题标题】:Return 1/2 for input values that are less than 0.05 - jQuery小于 0.05 的输入值返回 1/2 - jQuery
【发布时间】:2018-05-11 12:46:10
【问题描述】:

我正在尝试为<0.05 的所有输入值返回1/2。 (不要为数学而烦恼。我只想为所有<0.05 的值返回1/2)。

这是我正在处理的 sn-p:

function simpler(whole, x, y) {
  if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  $('#result').html(frac.toString());
}

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result"></div>

在示例中,我使用toFixed(1) 只保留输入的一位小数。因此,如果输入值为&lt;0.05,它什么也不返回。但我想返回1/2

【问题讨论】:

  • 我不完全理解您要做什么(例如 1/2 !== 0.05,您的示例将 0.25 转换为 2/5),但您可能会从这里受益:@ 987654321@
  • 感谢您的建议。无论实际数学是什么,我都希望输入 &lt;0.05 的输出值为 1/2。 @丹尼尔

标签: javascript jquery


【解决方案1】:

像这样简单的东西怎么样:(我在sn-p中使用过)

if ( '' === whole && 0 == x & 1 == y ) {
  return '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>';
}

或者只是:

if ( '' === whole ) {
  return '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>';
}

function simpler(whole, x, y) {
  if ( '' === whole && 0 == x & 1 == y ) {
    // If the input value is < 0.05, `whole` is an empty string with `x` = 0 and `y` = 1
    return '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  $('#result').html(frac.toString());
}

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result"></div>
0 or 0.00 would also return &frac12;

【讨论】:

  • 感谢您的建议。这是解决这个问题的完美方法。
【解决方案2】:

我不知道我是否得到了你想要实现的一切。但是这个呢:

基本上我只是定义一个修复模板 (var low_num_template) 并在值低于 0.05 时将其分配给您的 #result 框(假设您只有此输入中的数字。如果有字母字符,您必须在使用 parseFloat() 之前先检查以防止出错)。

我说得对吗?

function simpler(whole, x, y) {
  if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  text = parseFloat(text);
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  
  var low_num_template = '<sup>1</sup>/<sub>2</sub>';
  $('#result').html((text < 0.05) ? low_num_template : frac.toString());
  
}

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result">asdasd</div>

【讨论】:

  • 感谢您的建议,但如果我们可以在function simpler 中使用一些逻辑,那会不会更简洁,也许像if (x == 0 &amp;&amp; y == 0 &amp;&amp; whole == 0) ... return whole + '&lt;sup&gt;' + 1 + '&lt;/sup&gt;/&lt;sub&gt;' + 2 + '&lt;/sub&gt;' ... 或其他东西像这样真的有效吗?
【解决方案3】:

function simpler(whole, x, y) {
  if (x == 0) {
    return whole;
  } else if (x == 1 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 2 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 3 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 4 && y == 10) {
    return whole + '<sup>' + 2 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 5 && y == 10) {
    return whole + '<sup>' + 1 + '</sup>/<sub>' + 2 + '</sub>'
  } else if (x == 6 && y == 10) {
    return whole + '<sup>' + 3 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 7 && y == 10) {
    return whole + '<sup>' + 7 + '</sup>/<sub>' + 10 + '</sub>'
  } else if (x == 8 && y == 10) {
    return whole + '<sup>' + 4 + '</sup>/<sub>' + 5 + '</sub>'
  } else if (x == 9 && y == 10) {
    return whole + '<sup>' + 9 + '</sup>/<sub>' + 10 + '</sub>'
  } else {
    return whole + '<sup>' + x + '</sup>/<sub>' + y + '</sub>';
  }
}

function Fraction() {}
Fraction.prototype.convert = function(x, improper) {
  improper = improper || false;
  var abs = Math.abs(x);
  this.sign = x / abs;
  x = abs;
  var stack = 0;
  this.whole = !improper ? Math.floor(x) : 0;
  var fractional = !improper ? x - this.whole : abs;

  function recurs(x) {
    stack++;
    var intgr = Math.floor(x);
    var dec = (x - intgr);
    if (dec < 0.0019 || stack > 20) return [intgr, 1];
    var num = recurs(1 / dec);
    return [intgr * num[0] + num[1], num[0]]
  }
  var t = recurs(fractional);
  this.numerator = t[0];
  this.denominator = t[1];
}

Fraction.prototype.toString = function() {
  var l = this.sign.toString().length;
  var sign = l === 2 ? '-' : '';
  var whole = this.whole !== 0 ? this.sign * this.whole + ' ' : sign;
  return simpler(whole, this.numerator, this.denominator);

}

function f() {
  var text = $('#text').val();
  var roundUp = 0.4;
  var digit = (text * 1).toFixed(1);
  var frac = new Fraction()
  frac.convert(digit, false)
  if(parseInt(text)<0.05){
      $('#result').html("1/2");
   }
 else{
  $('#result').html(frac.toString());}
  }

$('#text').on('change', function() {
  f();
});

f();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="text" value="0.04" />
<div id="result"></div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-27
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多