Skip to content

Convert

number_to_hebrew_string(number, punctuate=True, geresh=True, substitution_functions=Substitutions.DEFAULT) ל

Convert a number into its Hebrew letter form.

Parameters:

  • number (int) –

    The number to convert to Hebrew letters. Must be greater than 0.

  • punctuate (bool, default: True ) –

    Whether to add punctuation in the appropriate places.

  • geresh (bool, default: True ) –

    If punctuate is true, whether to use the unicode geresh or an apostrophe.

  • substitution_functions (Optional[Tuple[Callable[[str], str], ...]], default: DEFAULT ) –

    A tuple of functions that replaces some hebrew values in the result with an appropriate equivalent. By default, "יה" and "יו" are replaced with "טו" and "טז" respectively. To replace all values such as שמד ,רע, and others, use Substitutions.ALL.

Returns:

  • str
Source code in hebrew/numerical_conversion/convert.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def number_to_hebrew_string(
    number: int,
    punctuate: bool = True,
    geresh: bool = True,
    substitution_functions: Optional[
        Tuple[Callable[[str], str], ...]
    ] = Substitutions.DEFAULT,
) -> str:
    """
    Convert a number into its Hebrew letter form.

    :param number: The number to convert to Hebrew letters. Must be greater than 0.
    :param punctuate: Whether to add punctuation in the appropriate places.
    :param geresh: If punctuate is true, whether to use the unicode geresh or an apostrophe.
    :param substitution_functions: A tuple of functions that replaces some hebrew values in the result with an
    appropriate equivalent. By default, "יה" and "יו" are replaced with "טו" and "טז" respectively. To replace all
    values such as שמד ,רע, and others, use `Substitutions.ALL`.
    :return:
    """
    # Handle 0
    if number < 1:
        raise ValueError("Number must be greater than 0")

    reversed_result = ""

    # Prepare the numbers
    ones_value = _ones_column_value(number)
    if ones_value > 0:
        reversed_result += HEBREW_LETTER_TO_VALUE_MAPPINGS[ones_value]
    tens_value = _tens_column_value(number)
    if tens_value > 0:
        reversed_result += HEBREW_LETTER_TO_VALUE_MAPPINGS[tens_value]
    hundreds_value = _hundreds_and_above_column_value(number)
    if hundreds_value > 0:
        reversed_result += _hundreds_to_letters(hundreds_value)

    # Reverse the string
    result = reversed_result[::-1]

    # Substitute flags
    if substitution_functions:
        for func in substitution_functions:
            result = func(result)

    # Add Punctuation
    if punctuate:
        if len(result) > 1:
            punctuation = "״" if geresh else '"'
            result = result[:-1] + punctuation + result[-1]
        else:
            punctuation = "׳" if geresh else "'"
            result += punctuation

    return result