• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

savoirfairelinux / num2words / 6098159229

06 Sep 2023 02:02PM UTC coverage: 97.424% (+4.8%) from 92.579%
6098159229

Pull #492

github

web-flow
Merge 3e39091d0 into f2fb5bc67
Pull Request #492: 2 Issues Potentially Fixed

2978 of 3140 branches covered (0.0%)

Branch coverage included in aggregate %.

3683 of 3696 new or added lines in 84 files covered. (99.65%)

3 existing lines in 1 file now uncovered.

8102 of 8233 relevant lines covered (98.41%)

4.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.18
/num2words/lang_FA.py
1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2003, Taro Ogawa.  All Rights Reserved.
3
# Copyright (c) 2013, Savoir-faire Linux inc.  All Rights Reserved.
4
# Copyright (c) 2018, Abdullah Alhazmy, Alhazmy13.  All Rights Reserved.
5
# Copyright (c) 2020, Hamidreza Kalbasi.  All Rights Reserved.
6
# Copyright (c) 2023, Nika Soltani Tehrani.  All Rights Reserved.
7

8
# This library is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU Lesser General Public
10
# License as published by the Free Software Foundation; either
11
# version 2.1 of the License, or (at your option) any later version.
12
# This library is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
# Lesser General Public License for more details.
16
# You should have received a copy of the GNU Lesser General Public
17
# License along with this library; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
# MA 02110-1301 USA
20

21
from decimal import Decimal
5✔
22
from math import floor
5✔
23

24
farsiOnes = [
5✔
25
    "", "یک", "دو", "سه", "چهار", "پنج", "شش", "هفت", "هشت",
26
    "نه",
27
    "ده",
28
    "یازده",
29
    "دوازده",
30
    "سیزده",
31
    "چهارده",
32
    "پانزده",
33
    "شانزده",
34
    "هفده",
35
    "هجده",
36
    "نوزده",
37
]
38

39
farsiTens = [
5✔
40
    "",
41
    "ده",
42
    "بیست",
43
    "سی",
44
    "چهل",
45
    "پنجاه",
46
    "شصت",
47
    "هفتاد",
48
    "هشتاد",
49
    "نود",
50
]
51

52
farsiHundreds = [
5✔
53
    "",
54
    "صد",
55
    "دویست",
56
    "سیصد",
57
    "چهارصد",
58
    "پانصد",
59
    "ششصد",
60
    "هفتصد",
61
    "هشتصد",
62
    "نهصد",
63
]
64

65
farsiBig = [
5✔
66
    '',
67
    ' هزار',
68
    ' میلیون',
69
    " میلیارد",
70
    ' تریلیون',
71
    " تریلیارد",
72
]
73

74
farsiFrac = ["", "دهم", "صدم"]
5✔
75
farsiFracBig = ["", "هزارم", "میلیونیم", "میلیاردیم"]
5✔
76

77
farsiSeperator = ' و '
5✔
78

79

80
class Num2Word_FA(object):
5✔
81
    # Those are unused
82
    errmsg_toobig = "Too large"
5✔
83
    MAXNUM = 10 ** 36
5✔
84

85
    def __init__(self):
5✔
86
        self.number = 0
5✔
87

88
    def float2tuple(self, value):
5✔
89
        pre = int(value)
5✔
90

91
        # Simple way of finding decimal places to update the precision
92
        self.precision = abs(Decimal(str(value)).as_tuple().exponent)
5✔
93

94
        post = abs(value - pre) * 10**self.precision
5✔
95
        if abs(round(post) - post) < 0.01:
5!
96
            # We generally floor all values beyond our precision (rather than
97
            # rounding), but in cases where we have something like 1.239999999,
98
            # which is probably due to python's handling of floats, we actually
99
            # want to consider it as 1.24 instead of 1.23
100
            post = int(round(post))
5✔
101
        else:
NEW
102
            post = int(floor(post))
×
103
        return pre, post, self.precision
5✔
104

105
    def cardinal3(self, number):
5✔
106
        if number <= 19:
5✔
107
            return farsiOnes[number]
5✔
108
        if number < 100:
5✔
109
            x, y = divmod(number, 10)
5✔
110
            if y == 0:
5✔
111
                return farsiTens[x]
5✔
112
            return farsiTens[x] + farsiSeperator + farsiOnes[y]
5✔
113
        x, y = divmod(number, 100)
5✔
114
        if y == 0:
5✔
115
            return farsiHundreds[x]
5✔
116
        return farsiHundreds[x] + farsiSeperator + self.cardinal3(y)
5✔
117

118
    def cardinalPos(self, number):
5✔
119
        x = number
5✔
120
        res = ''
5✔
121
        for b in farsiBig:
5✔
122
            x, y = divmod(x, 1000)
5✔
123
            if y == 0:
5✔
124
                continue
5✔
125
            yx = self.cardinal3(y) + b
5✔
126
            if b == ' هزار' and y == 1:
5✔
127
                yx = 'هزار'
5✔
128
            if res == '':
5✔
129
                res = yx
5✔
130
            else:
131
                res = yx + farsiSeperator + res
5✔
132
        return res
5✔
133

134
    def fractional(self, number, level):
5✔
135
        if number == 5:
5✔
136
            return "نیم"
5✔
137
        x = self.cardinalPos(number)
5✔
138
        ld3, lm3 = divmod(level, 3)
5✔
139
        ltext = (farsiFrac[lm3] + " " + farsiFracBig[ld3]).strip()
5✔
140
        return x + " " + ltext
5✔
141

142
    def to_currency(self, value):
5✔
143
        return self.to_cardinal(value) + " تومان"
5✔
144

145
    def to_ordinal(self, number):
5✔
146
        r = self.to_cardinal(number)
5✔
147
        if r[-1] == 'ه' and r[-2] == 'س':
5✔
148
            return r[:-1] + 'وم'
5✔
149
        return r + 'م'
5✔
150

151
    def to_year(self, value):
5✔
152
        return self.to_cardinal(value)
5✔
153

154
    @staticmethod
5✔
155
    def to_ordinal_num(value):
4✔
156
        return str(value)+"م"
5✔
157

158
    def to_cardinal(self, number):
5✔
159
        if number < 0:
5✔
160
            return "منفی " + self.to_cardinal(-number)
5✔
161
        if number == 0:
5✔
162
            return "صفر"
5✔
163
        x, y, level = self.float2tuple(number)
5✔
164
        if y == 0:
5✔
165
            return self.cardinalPos(x)
5✔
166
        if x == 0:
5✔
167
            return self.fractional(y, level)
5✔
168
        return self.cardinalPos(x) + farsiSeperator + self.fractional(y, level)
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc