22
22
class DecimalNumber (VMobject , metaclass = ConvertToOpenGL ):
23
23
"""An mobject representing a decimal number.
24
24
25
+ Parameters
26
+ ----------
27
+ number
28
+ The numeric value to be displayed. It can later be modified using :meth:`.set_value`.
29
+ num_decimal_places
30
+ The number of decimal places after the decimal separator. Values are automatically rounded.
31
+ mob_class
32
+ The class for rendering digits and units, by default :class:`.MathTex`.
33
+ include_sign
34
+ Set to ``True`` to include a sign for positive numbers and zero.
35
+ group_with_commas
36
+ When ``True`` thousands groups are separated by commas for readability.
37
+ digit_buff_per_font_unit
38
+ Additional spacing between digits. Scales with font size.
39
+ show_ellipsis
40
+ When a number has been truncated by rounding, indicate with an ellipsis (``...``).
41
+ unit
42
+ A unit string which can be placed to the right of the numerical values.
43
+ unit_buff_per_font_unit
44
+ An additional spacing between the numerical values and the unit. A value
45
+ of ``unit_buff_per_font_unit=0.003`` gives a decent spacing. Scales with font size.
46
+ include_background_rectangle
47
+ Adds a background rectangle to increase contrast on busy scenes.
48
+ edge_to_fix
49
+ Assuring right- or left-alignment of the full object.
50
+ font_size
51
+ Size of the font.
52
+
25
53
Examples
26
54
--------
27
55
@@ -34,6 +62,8 @@ def construct(self):
34
62
show_ellipsis=True,
35
63
num_decimal_places=3,
36
64
include_sign=True,
65
+ unit=r"\t ext{M-Units}",
66
+ unit_buff_per_font_unit=0.003
37
67
)
38
68
square = Square().to_edge(UP)
39
69
@@ -59,6 +89,7 @@ def __init__(
59
89
digit_buff_per_font_unit : float = 0.001 ,
60
90
show_ellipsis : bool = False ,
61
91
unit : str | None = None , # Aligned to bottom unless it starts with "^"
92
+ unit_buff_per_font_unit : float = 0 ,
62
93
include_background_rectangle : bool = False ,
63
94
edge_to_fix : Sequence [float ] = LEFT ,
64
95
font_size : float = DEFAULT_FONT_SIZE ,
@@ -75,6 +106,7 @@ def __init__(
75
106
self .digit_buff_per_font_unit = digit_buff_per_font_unit
76
107
self .show_ellipsis = show_ellipsis
77
108
self .unit = unit
109
+ self .unit_buff_per_font_unit = unit_buff_per_font_unit
78
110
self .include_background_rectangle = include_background_rectangle
79
111
self .edge_to_fix = edge_to_fix
80
112
self ._font_size = font_size
@@ -89,6 +121,7 @@ def __init__(
89
121
"digit_buff_per_font_unit" : digit_buff_per_font_unit ,
90
122
"show_ellipsis" : show_ellipsis ,
91
123
"unit" : unit ,
124
+ "unit_buff_per_font_unit" : unit_buff_per_font_unit ,
92
125
"include_background_rectangle" : include_background_rectangle ,
93
126
"edge_to_fix" : edge_to_fix ,
94
127
"font_size" : font_size ,
@@ -130,15 +163,25 @@ def _set_submobjects_from_number(self, number):
130
163
self ._string_to_mob ("\\ dots" , SingleStringMathTex , color = self .color ),
131
164
)
132
165
133
- if self .unit is not None :
134
- self .unit_sign = self ._string_to_mob (self .unit , SingleStringMathTex )
135
- self .add (self .unit_sign )
136
-
137
166
self .arrange (
138
167
buff = self .digit_buff_per_font_unit * self ._font_size ,
139
168
aligned_edge = DOWN ,
140
169
)
141
170
171
+ if self .unit is not None :
172
+ self .unit_sign = self ._string_to_mob (self .unit , SingleStringMathTex )
173
+ self .add (
174
+ self .unit_sign .next_to (
175
+ self ,
176
+ direction = RIGHT ,
177
+ buff = (self .unit_buff_per_font_unit + self .digit_buff_per_font_unit )
178
+ * self ._font_size ,
179
+ aligned_edge = DOWN ,
180
+ )
181
+ )
182
+
183
+ self .move_to (ORIGIN )
184
+
142
185
# Handle alignment of parts that should be aligned
143
186
# to the bottom
144
187
for i , c in enumerate (num_string ):
0 commit comments