File size: 830 Bytes
5afd2f7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
"""Display grid of numeric options: one decimal place, for the stored value and the text alike."""
from dataclasses import dataclass


@dataclass(frozen=True)
class Format:
    def value(self, value):
        """Round to the displayed decimal (same rounding as text), normalizing signed zero: stored value == shown value."""
        if isinstance(value, list):
            return [self.value(x) for x in value]
        rounded = float(f"{value:.1f}")
        return 0. if rounded == 0 else rounded

    def text(self, value):
        """Plain decimal with one digit after the point (options are already quantized), also per vector component."""
        if isinstance(value, list):
            return "[" + ", ".join(self.text(x) for x in value) + "]"
        text = f"{value:.1f}"
        return "0.0" if text == "-0.0" else text