浮点型精度

pull/1/head
韩天峰 3 years ago
parent 4b9f52e466
commit f7018ddf8e
  1. 3
      cases/float.py
  2. 18
      conv.php
  3. 11
      web/public/index.php
  4. 2
      web/public/input.php
  5. 2
      web/public/output.php
  6. 3
      web/public/static/css/style.css

@ -0,0 +1,3 @@
price_val = 6.12658
world = "swoole"
print(f'{price_val:.2f}, hello={world}') # 6.13

@ -263,7 +263,7 @@ class Translator
case 'Subscript':
return $this->parseSubscript($value);
case 'FormattedValue':
return $this->parseValue($value->value);
return $this->parseFormattedValue($value);
case 'UnaryOp':
return $this->parseTarget($value);
case 'BoolOp':
@ -921,6 +921,22 @@ class Translator
$code = 'return ' . $lines[0] . PHP_EOL;
return $code . PHP_EOL . $this->getIndent() . '}';
}
private function parseFormattedValue($value)
{
$expr = $this->parseValue($value->value);
if ($value->format_spec and $value->format_spec->_type == 'JoinedStr') {
if (count($value->format_spec->values) != 1) {
debug($value);
}
$format = $value->format_spec->values[0]->value;
if ($format[0] == '.' and $format[-1] == 'f') {
return 'round(' . $expr . ', ' . substr($format, 1, strlen($format) - 2) . ')';
}
} else {
return $expr;
}
}
}
error_reporting(E_ERROR);

@ -20,11 +20,12 @@
<iframe src="./input.php" id="iframe-input" class="code-iframe"></iframe>
<iframe src="./output.php" width="50%" id="iframe-output"></iframe>
<script>
document.getElementById('iframe-input').height = window.screen.height
document.getElementById('iframe-output').height = window.screen.height
const sub = 60
document.getElementById('iframe-input').width = window.screen.width / 2 - sub
document.getElementById('iframe-output').width = window.screen.width / 2 - sub
const wsub = 60
const hsub = 100
document.getElementById('iframe-input').height = window.screen.height - hsub
document.getElementById('iframe-output').height = window.screen.height - hsub
document.getElementById('iframe-input').width = window.screen.width / 2 - wsub
document.getElementById('iframe-output').width = window.screen.width / 2 - wsub
</script>
</body>
</html>

@ -83,6 +83,8 @@ define('ROOT_PATH', dirname(__DIR__));
window.parent.frames[1].updateCode()
}
$('#divCodeWrapper').css('height', (window.screen.height - 30) + 'px')
$('#btn-convert').click(function () {
const code = textarea1.value

@ -65,6 +65,8 @@ define('ROOT_PATH', dirname(__DIR__));
$('#btn-copy').attr('disabled', 'disabled').text('已复制代码')
})
$('#divCodeWrapper').css('height', (window.screen.height - 30) + 'px')
function updateLineNumbers() {
let lineCount = textarea1.value.split('\n').length;
let lines = '';

@ -3,7 +3,6 @@ select {
}
#divCodeWrapper {
height: calc(100vh - 200px);
max-height: 800px;
width: 1200px;
overflow: hidden;
@ -41,7 +40,7 @@ textarea {
padding: 15px;
z-index: 2;
overflow-x: auto;
overflow-y: scroll;
overflow-y: auto;
white-space: nowrap;
background-color: rgba(0, 0, 0, 0);
color: rgba(0, 0, 0, 0);

Loading…
Cancel
Save