Fixed visible floating point errors in number fields. (#2384)

This commit is contained in:
Beka Westberg
2019-04-15 13:26:20 -07:00
committed by RoboErikG
parent 6bb27052ee
commit f88c704d69
3 changed files with 51 additions and 10 deletions

View File

@@ -79,6 +79,10 @@ Blockly.FieldNumber.fromJson = function(options) {
Blockly.FieldNumber.prototype.setConstraints = function(min, max, precision) {
precision = parseFloat(precision);
this.precision_ = isNaN(precision) ? 0 : precision;
var precisionString = this.precision_.toString();
var decimalIndex = precisionString.indexOf('.');
this.fractionalDigits_ = (decimalIndex == -1) ? -1 :
precisionString.length - (decimalIndex + 1);
min = parseFloat(min);
this.min_ = isNaN(min) ? -Infinity : min;
max = parseFloat(max);
@@ -106,13 +110,14 @@ Blockly.FieldNumber.prototype.classValidator = function(text) {
// Invalid number.
return null;
}
// Get the value in range.
n = Math.min(Math.max(n, this.min_), this.max_);
// Round to nearest multiple of precision.
if (this.precision_ && isFinite(n)) {
n = Math.round(n / this.precision_) * this.precision_;
}
// Get the value in range.
n = Math.min(Math.max(n, this.min_), this.max_);
return String(n);
return (this.fractionalDigits_ == -1) ? String(n) :
n.toFixed(this.fractionalDigits_);
};
Blockly.Field.register('field_number', Blockly.FieldNumber);

View File

@@ -207,8 +207,8 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
"tooltip": "A number."
},
{
"type": "test_fields_integer",
"message0": "integer %1",
"type": "test_fields_number_whole",
"message0": "precision 1 %1",
"args0": [
{
"type": "field_number",
@@ -219,11 +219,11 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
],
"style": "math_blocks",
"output": "Number",
"tooltip": "An integer."
"tooltip": "The number should be rounded to multiples of 1"
},
{
"type": "test_fields_number_hundredths",
"message0": "$ %1",
"message0": "precision 0.01 %1",
"args0": [
{
"type": "field_number",
@@ -234,7 +234,37 @@ Blockly.defineBlocksWithJsonArray([ // BEGIN JSON EXTRACT
],
"style": "math_blocks",
"output": "Number",
"tooltip": "A dollar amount."
"tooltip": "The number should be rounded to multiples of 0.01"
},
{
"type": "test_fields_number_halves",
"message0": "precision 0.5 %1",
"args0": [
{
"type": "field_number",
"name": "NUM",
"precision": 0.5,
"text": "0"
}
],
"style": "math_blocks",
"output": "Number",
"tooltip": "The number should be rounded to multiples of 0.5"
},
{
"type": "test_fields_number_three_halves",
"message0": "precision 1.5 %1",
"args0": [
{
"type": "field_number",
"name": "NUM",
"precision": 1.5,
"text": "0"
}
],
"style": "math_blocks",
"output": "Number",
"tooltip": "The number should be rounded to multiples of 1.5"
},
{
"type": "test_fields_integer_bounded",

View File

@@ -1197,10 +1197,16 @@ h1 {
<block type="test_fields_number">
<field name="NUM">123.456</field>
</block>
<block type="test_fields_integer">
<block type="test_fields_number_hundredths">
<field name="NUM">123.456</field>
</block>
<block type="test_fields_number_hundredths">
<block type="test_fields_number_halves">
<field name="NUM">123.456</field>
</block>
<block type="test_fields_number_whole">
<field name="NUM">123.456</field>
</block>
<block type="test_fields_number_three_halves">
<field name="NUM">123.456</field>
</block>
<block type="test_fields_integer_bounded">