English 中文(简体)
印古兰经中流达一字
原标题:Convert amount to word in Python In Indian Format
  • 时间:2019-10-21 12:58:05
  •  标签:
  • python

How to convert amount to word in Indian?

我正在使用N2words 图书馆,但该图书馆在提出“ la”和“ores”。

例如:

num2words(903614.55, lang= en-IN ) Its printing nine hundred and three thousand, six hundred and fourteen point five five

但是,印度实际数额的列报方式应为:nine lakhs 3 000, 640 14 and five paisa

随后,我尝试了以下法典:

def num2words(num):
    under_20 = [ Zero , One , Two , Three , Four , Five , Six , Seven , Eight , Nine , Ten , Eleven , Twelve , Thirteen , Fourteen , Fifteen , Sixteen , Seventeen , Eighteen , Nineteen ]
    tens = [ Twenty , Thirty , Forty , Fifty , Sixty , Seventy , Eighty , Ninety ]
    above_100 = {100:  Hundred ,1000: Thousand , 100000: Lakhs , 10000000: Crores }

    if num < 20:
         return under_20[num]

    if num < 100:
        return tens[(int)(num/10)-2] + (   if num%10==0 else     + under_20[num%10])

    # find the appropriate pivot -  Million  in 3,603,550, or  Thousand  in 603,550
    pivot = max([key for key in above_100.keys() if key <= num])

    return num2words((int)(num/pivot)) +     + above_100[pivot] + (   if num%pivot==0 else     + num2words(num%pivot))

但现在出现错误

类型:清单指标必须是分类账或斜体,而不是小数。 Decimal

我的问题是人数少,Integer正在做罚款。

最佳回答

I guess you could just cast it on the first line if you re not handling decimals. You can also use // to do integer division.

import decimal    

def num2words(num):
    num = decimal.Decimal(num)
    decimal_part = num - int(num)
    num = int(num)

    if decimal_part:
        return num2words(num) + " point " + (" ".join(num2words(i) for i in str(decimal_part)[2:]))

    under_20 = [ Zero ,  One ,  Two ,  Three ,  Four ,  Five ,  Six ,  Seven ,  Eight ,  Nine ,  Ten ,  Eleven ,  Twelve ,  Thirteen ,  Fourteen ,  Fifteen ,  Sixteen ,  Seventeen ,  Eighteen ,  Nineteen ]
    tens = [ Twenty ,  Thirty ,  Forty ,  Fifty ,  Sixty ,  Seventy ,  Eighty ,  Ninety ]
    above_100 = {100:  Hundred , 1000:  Thousand , 100000:  Lakhs , 10000000:  Crores }

    if num < 20:
        return under_20[num]

    if num < 100:
        return tens[num // 10 - 2] + (   if num % 10 == 0 else     + under_20[num % 10])

    # find the appropriate pivot -  Million  in 3,603,550, or  Thousand  in 603,550
    pivot = max([key for key in above_100.keys() if key <= num])

    return num2words(num // pivot) +     + above_100[pivot] + (   if num % pivot==0 else     + num2words(num % pivot))


print(num2words(decimal.Decimal("650958.32")))
# Six Lakhs Fifty Thousand Nine Hundred Fifty Eight point Three Two
问题回答

解决办法简单,错误在后瞻中显而易见。

仅以“IN”代替“N2words”。 没有必要制定任何习惯法。 我也被烧毁。

num2words.num2words(123456, lang= en_IN )

回返

one lakh, twenty-three thousand, four hundred and fifty-six

您可以分立精细和分数部分,可以把南2字数的两倍换成单数,而其他则分部分。

import math
def num2words(num):
    under_20 = [ Zero , One , Two , Three , Four , Five , Six , Seven , Eight , Nine , Ten , Eleven , Twelve , Thirteen , Fourteen , Fifteen , Sixteen , Seventeen , Eighteen , Nineteen ]
    tens = [ Twenty , Thirty , Forty , Fifty , Sixty , Seventy , Eighty , Ninety ]
    above_100 = {100:  Hundred ,1000: Thousand , 100000: Lakhs , 10000000: Crores }

    if num < 20:
         return under_20[(int)(num)]

    if num < 100:
        return tens[(int)(num/10)-2] + (   if num%10==0 else     + under_20[(int)(num%10)])

    # find the appropriate pivot -  Million  in 3,603,550, or  Thousand  in 603,550
    pivot = max([key for key in above_100.keys() if key <= num])

    return num2words((int)(num/pivot)) +     + above_100[pivot] + (   if num%pivot==0 else     + num2words(num%pivot))

num="5.12"
print(num2words(int(num.split(".")[0])))
print(num2words(int(num.split(".")[1])))

https://ide.geeksforgeeks.org/J7zsZyIT6m

如果你不实写明的货币数额,就会尝试:

num2words(amount, lang= en_IN , to= currency , currency= USD )

它使用N2字(人数的精华译员)。 你只是提供数额(如50.75美元)和任择货币(如美元“美元”),法典将写给你。 “美元和七十五美分”





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签