📜  Python数值器库

📅  最后修改于: 2022-05-13 01:55:32.221000             🧑  作者: Mango

Python数值器库

Numerizer是一个库,它将根据国际数字系统转换用英文书写的数字。它可以转换由一个周期、千周期、百万周期和万亿周期四个周期组成的输入。每个句点由三位数字组成,即个位、十位和百位。它是一个广泛使用的自然语言处理和数据科学库。

注意:要转换印度数字系统的数字,您必须先将其写入International Number System

安装库:

pip install numerizer 

示例 1:

Python3
# Importing Numerize function
# From Numerizer Library
from numerizer import numerize
 
  
# We can get integer value
# in output by converting explicitly
a = int(numerize('Three'))
print(a)
print(numerize('five thousand two hundred and twenty'))
 
 
# If we are not converting our
# output explicitly into integer
# then by default it will return
# a string value
a = numerize('forty four thousand four hundred forty four')
print(a)
print("Type", type(a))
  
a = numerize('sixty billion forty million twenty thousand four hundred six')
print(a)


Python3
# Importing Numerize function
# From Numerizer Library
from numerizer import numerize
 
 
# here we can also pass numerical
# values with corresponding periods
# it will still give same output
a = numerize('320 thousand three hundred twenty ')
print(a)
 
a = numerize('990 trillion 988 billion 881 million 999 thousand nine hundred ninety nine')
print(a)
  
# here we can also get float values
# in our output by using "half" and
# "quarter" terms in input
a = numerize('twenty nine one half')
print(a)
type(a)
  
# here we are explicitly converting
# our output into float type
a = float(numerize(' nine hundred ninety and two half'))
 
# it will add two half
# (i.e. 1/2+1/2) to our no
# which will add 1 to our answer
print(a)
type(a)
  
a = numerize('two thousand four hundred twenty and three quarters')
 
# it will add (3/4) to our answer
print(a)


输出 :

3
5220
44444
Type 
60040020406

示例 2:

Python3

# Importing Numerize function
# From Numerizer Library
from numerizer import numerize
 
 
# here we can also pass numerical
# values with corresponding periods
# it will still give same output
a = numerize('320 thousand three hundred twenty ')
print(a)
 
a = numerize('990 trillion 988 billion 881 million 999 thousand nine hundred ninety nine')
print(a)
  
# here we can also get float values
# in our output by using "half" and
# "quarter" terms in input
a = numerize('twenty nine one half')
print(a)
type(a)
  
# here we are explicitly converting
# our output into float type
a = float(numerize(' nine hundred ninety and two half'))
 
# it will add two half
# (i.e. 1/2+1/2) to our no
# which will add 1 to our answer
print(a)
type(a)
  
a = numerize('two thousand four hundred twenty and three quarters')
 
# it will add (3/4) to our answer
print(a)

输出:

320320 
990988881999999
29.5
991.0
2420.75

注意: Numerize 总是将字符串作为输入,否则会引发 SyntaxError。