Skip to content

Commit afe721f

Browse files
committed
Added strict float capture variants normalizing comma.
1 parent 7bd71fd commit afe721f

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

core/numbers/numbers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,25 +308,37 @@ def number_signed_small(m) -> int:
308308
rule="[<user.number_string>] (point | comma) <digit_string> | <user.number_string>"
309309
)
310310
def number_float_string(m) -> str:
311-
"""Parses a phrase for a floating-point number, returning the number as a string."""
311+
"""Parses a phrase for a floating-point number, returning the number as a string with one of different possible decimal separators."""
312312
decimal_separator = "." if "point" in m else ","
313313
return f"{getattr(m, 'number_string', '0')}{decimal_separator}{getattr(m, 'digit_string', '0')}"
314314

315315

316316
@mod.capture(rule="<user.number_float_string>")
317+
def number_strict_float_string(m) -> str:
318+
"""Parses a phrase for a floating-point number, returning the number as a string with point as decimal separator."""
319+
return m.number_float_string.replace(",", ".", 1)
320+
321+
322+
@mod.capture(rule="<user.number_strict_float_string>")
317323
def number_float(m) -> float:
318-
"""Parses a phrase for an unsigned floating-point number. Shouldn't be used for text output, because long numbers will be rounded."""
319-
return float(m.number_float_string.replace(",", ".", 1))
324+
"""Parses a phrase for an unsigned floating-point number. Shouldn't be used for text output, because long numbers will be rounded, and, depending on the requirements, because the decimal separator is normalized."""
325+
return float(m.number_strict_float_string)
320326

321327

322328
@mod.capture(rule="[negative | minus] <user.number_float_string>")
323329
def number_float_signed_string(m) -> str:
324-
"""Parses a phrase for a floating-point number that may be negative, returning the number as a string."""
330+
"""Parses a phrase for a floating-point number that may be negative, returning the number as a string with one of different possible decimal separators."""
325331
number = m.number_float_string
326332
return f"-{number}" if m[0] in ["negative", "minus"] else number
327333

328334

329335
@mod.capture(rule="<user.number_float_signed_string>")
336+
def number_strict_float_signed_string(m) -> str:
337+
"""Parses a phrase for a floating-point number that may be negative, returning the number as a string with point as decimal separator."""
338+
return m.number_float_signed_string.replace(",", ".", 1)
339+
340+
341+
@mod.capture(rule="<user.number_strict_float_signed_string>")
330342
def number_float_signed(m) -> float:
331-
"""Parses a phrase for a floating-point number that may be negative. Shouldn't be used for text output, because long numbers will be rounded."""
332-
return float(m.number_float_signed_string.replace(",", ".", 1))
343+
"""Parses a phrase for a floating-point number that may be negative. Shouldn't be used for text output, because long numbers will be rounded, and, depending on the requirements, because the decimal separator is normalized."""
344+
return float(m.number_strict_float_signed_string)

0 commit comments

Comments
 (0)