Skip to content

Commit 38bf597

Browse files
authored
Added Blockly block to format a numeric value (#1925)
1 parent ca64239 commit 38bf597

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Executes Javascript, Typescript Scripts.
2626
<!--
2727
### **WORK IN PROGRESS**
2828
-->
29+
### **WORK IN PROGRESS**
30+
* (@klein0r) Added Blockly block to format a numeric value
31+
2932
### 9.0.7 (2025-06-29)
3033
* (@GermanBluefox) Fixing some blocks in blockly: time, function
3134

src-editor/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,19 @@
480480
<shadow type="text">text</shadow>
481481
</value>
482482
</block>
483+
<block type="text_format_value">
484+
<value name="VALUE">
485+
<shadow type="math_number">
486+
<field name="NUM">1.23</field>
487+
</shadow>
488+
</value>
489+
<field name="FORMAT">system</field>
490+
<value name="DECIMALS">
491+
<shadow type="math_number">
492+
<field name="NUM">2</field>
493+
</shadow>
494+
</value>
495+
</block>
483496
</category>
484497
<category
485498
name="{catLists}"

src-editor/public/google-blockly/own/blocks_text.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,46 @@ Blockly.JavaScript.forBlock['text_contains'] = function (block) {
5959

6060
return [`String(${vValue}).includes(${vFind})`, Blockly.JavaScript.ORDER_ATOMIC];
6161
};
62+
63+
// --- Text formatValue --------------------------------------------------
64+
65+
Blockly.Blocks['text_format_value'] = {
66+
// Checkbox.
67+
init: function () {
68+
this.appendValueInput('VALUE')
69+
.appendField(Blockly.Translate('text_format_value'))
70+
.setCheck(null);
71+
72+
this.appendDummyInput()
73+
.appendField(Blockly.Translate('text_format_value_format'))
74+
.appendField(new Blockly.FieldDropdown([
75+
['System', 'system'],
76+
['.,', '.,'],
77+
[',.', ',.'],
78+
[' .', ' .'],
79+
]), 'FORMAT');
80+
81+
this.appendValueInput('DECIMALS')
82+
.appendField(Blockly.Translate('text_format_value_decimals'));
83+
84+
85+
this.setInputsInline(true);
86+
this.setOutput(true, 'String');
87+
88+
this.setColour('%{BKY_TEXTS_HUE}');
89+
90+
//this.setTooltip(Blockly.Translate('text_format_value_tooltip'));
91+
},
92+
};
93+
94+
Blockly.JavaScript.forBlock['text_format_value'] = function (block) {
95+
const vValue = Blockly.JavaScript.valueToCode(block, 'VALUE', Blockly.JavaScript.ORDER_ATOMIC);
96+
const vDecimals = Blockly.JavaScript.valueToCode(block, 'DECIMALS', Blockly.JavaScript.ORDER_ATOMIC);
97+
const fFormat = block.getFieldValue('FORMAT');
98+
99+
if (fFormat !== 'system') {
100+
return [`formatValue(parseFloat(${vValue}), ${vDecimals}, '${fFormat}')`, Blockly.JavaScript.ORDER_ATOMIC];
101+
}
102+
103+
return [`formatValue(parseFloat(${vValue}), ${vDecimals})`, Blockly.JavaScript.ORDER_ATOMIC];
104+
};

src-editor/public/google-blockly/own/blocks_words.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)