ntwindia/ntwindia

将数字转换为印度英语单词。

v1.0.1 2017-08-09 18:05 UTC

This package is auto-updated.

Last update: 2024-09-14 00:44:13 UTC


README

Build Status Code Climate Test Coverage Issue Count

此类可以将数字转换为印度英语单词。适用于所有类型的数字,包括浮点数。一些示例。

示例

  • 10,000,00 - 十拉克
  • 3,478 - 三千四百七十八
  • 1,234,567,890 - 一百二十三亿四千五百六十七万八千九百
  • 5,024.78 - 五千零二十四点七八

安装

使用Composer

导航到您的项目目录并执行

composer require ntwindia/ntwindia

如果您尚未这样做,请包含composer的autoload.php文件。

require_once 'vendor/autoload.php';

手动安装

使用git克隆此仓库

git clone git@github.com:swashata/php-number-to-word-india.git

包含类

<?php
require_once 'src/NTWIndia.php';
require_once 'src/Exception/NTWIndiaInvalidNumber.php';
require_once 'src/Exception/NTWIndiaNumberOverflow.php';

使用方法

创建实例

<?php
$ntw = new \NTWIndia\NTWIndia();

转换值

<?php
echo $ntw->numToWord( 3104007200 );
// Will print Three Hundred Ten Crore Forty Lakh Seven Thousand Two Hundred

如果您的数字总是小于100,则使用numToWordSmall方法以减少内存使用。

<?php
echo $ntw->numToWordSmall( 99 );
// Will print Ninty Nine

方法

有两个方法可用

numToWord

将任何数字转换为单词,包括小数。小数以... And 986/1000的形式转换。您通常需要调用此方法。

参数

  • $number: integer|float 要生成单词值的数字。

返回

返回 string 数字的单词值。所有单词的第一个字母都为大写。

numToWordSmall

将小于100的数字转换为单词。如果数字大于99,则它将简单地调用numToWord

当您知道您的数字小于100时使用此方法以减少内存使用。

参数

  • $number: integer 要生成单词值的数字。它不接受浮点值,如果提供,则将其转换为整数。

返回

返回 string 数字的单词值。所有单词的第一个字母都为大写。

异常处理

根据条件抛出两个异常。

\NTWIndia\Exception\NTWIndiaInvalidNumber

如果您将一个变量传递给一个方法,而这个变量不是一个有效的数字(! is_numeric( $number )

\NTWIndia\Exception\NTWIndiaNumberOverflow

如果传递一个超过限制的数字。

  • 对于numToWord,它是PHP_MAX_INT
  • 对于numToWordSmall,它是99

翻译

要翻译,请替换公共变量

  • $hundred: 百词。默认为'Hundred'
  • $thousand: 千词。默认为'Thousand'
  • $lakh: 拉克词。默认为'Lakh'
  • $crore: 克罗尔词。默认为'Crore'
  • $and: 和词。默认为'And'
  • $numToWord: 包含数字到单词的映射。从02010的倍数到90

单元测试

单元测试通过composer提供。

$ cd /path/to/php-number-to-word-india/
$ composer update
$ vendor/bin/phpunit

它将输出类似以下的内容。

Test Case

您非常欢迎添加更多测试用例并向我发送pull request。

请遵循贡献指南

许可证

PHP Number to Word in Indian Style是在GPLv3下许可的

Copyright (C) 2017  Swashata Ghosh

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://gnu.ac.cn/licenses/>