ivanovsaleksejs/num-to-text

将数字或价格转换为多种语言中的文本表示。例如,437605变为"四万三千七百零五"。还有用于显示带货币的价格的类。

dev-master 2024-04-18 13:14 UTC

This package is auto-updated.

Last update: 2024-09-18 14:15:40 UTC


README

将数字或价格转换为多种语言中的文本表示。

支持的语言

目前,库支持以下语言

  • 英语
  • 俄语
  • 拉脱维亚语

法语和西班牙语正在开发中。

如何安装

composer require ivanovsaleksejs/num-to-text:dev-master

或添加到您的 composer.json

{
    "require": {
        "ivanovsaleksejs\num-to-text": "^2.*"
    }
}

然后

composer install

如何使用

include __DIR__ . '/vendor/autoload.php';

use ivanovsaleksejs\NumToText\Num;
use ivanovsaleksejs\NumToText\Price;

echo Num::toText(1234, 'EN') . "\n";
// Echoes 'one thousand two hundred thirty four'

echo Price::toText(123456.78, [['dollars', 'dollar'], ['cents', 'cent']], 'EN', true) . "\n";
// Echoes 'one hundred twenty three thousand four hundred fifty six dollars 78 cents'

echo Price::toText(123456.78, [['dollars', 'dollar'], ['cents', 'cent']], 'EN') . "\n";
// Echoes 'one hundred twenty three thousand four hundred fifty six dollars seventy eight cents'


echo Price::toText(1.02, [['рублей', 'рубль', 'рубля'], ['копеек', 'копейка', 'копейки']], 'RU', false, false, [0, 1]); . "\n";
// Echoes 'один рубль две копейки'
// Please note, the sixth parameter specifies the gender for the integer and decimal parts of the number respectively
// 0 - masculine
// 1 - feminine
// 2 - neutral

如何添加新语言

要添加语言,您需要

  • 扩展主类 NumToText(为了确保简短函数正常工作,在新的类名中在下划线后添加语言的代码,例如,NumToText_DE)
  • 定义函数 digitToWordtoWords
  • 如有必要,覆盖主类中的其他一些函数。

如何运行单元测试

./vendor/bin/phpunit