Giter Club home page Giter Club logo

laravel-number-to-words's Introduction

Laravel Number To Words


Latest version Build status Quantity score StyleCI Total download License

Thông tin

Laravel number to words hổ trợ chuyển đổi số sang chữ số Tiếng Việt.

Cài đặt

Cài đặt Laravel Number To Words thông qua Composer:

composer require phpviet/laravel-number-to-words

Cách sử dụng

Các tính năng của extension:

Chuyển đổi số sang chữ số

  • Sử dụng thông qua facade N2W:
use N2W;

// âm năm
N2W::toWords(-5); 

// năm
N2W::toWords(5); 

// năm phẩy năm
N2W::toWords(5.5); 
  • Sử dụng thông qua hàm hổ trợ n2w:
// mười lăm
n2w(15); 

// một trăm linh năm
n2w(105); 

// hai mươi tư
n2w(24); 

Chuyển đổi số sang tiền tệ

  • Sử dụng thông qua facade N2W:
use N2W;

// năm triệu sáu trăm chín mươi nghìn bảy trăm đồng
N2W::toCurrency(5690700);
  • Sử dụng thông qua hàm hổ trợ n2c:
// chín mươi lăm triệu năm trăm nghìn hai trăm đồng
n2c(95500200);

Ngoài ra ta còn có thể sử dụng đơn vị tiền tệ khác thông qua tham trị thứ 2 của phương thức toCurrency và hàm n2c với mảng phần từ đầu tiên là đơn vị cho số nguyên và kế tiếp là đơn vị của phân số:

use N2W;

// sáu nghìn bảy trăm bốn mươi hai đô bảy xen
N2W::toCurrency(6742.7, ['đô', 'xen']);

// chín nghìn bốn trăm chín mươi hai đô mười lăm xen
n2c(9492.15, ['đô', 'xen']);

Thay cách đọc số

Nếu như bạn cảm thấy cách đọc ở trên ổn rồi thì hãy bỏ qua bước này.

Đầu tiên để thay đổi cách đọc số bạn cần phải publish file cấu hình thông qua câu lệnh:

php artisan vendor:publish --provider="PHPViet\Laravel\NumberToWords\ServiceProvider" --tag="config"

Sau khi publish xong ta sẽ có được file config config/n2w.php như sau:

return [
    /**
     * Cấu hình từ điển mặc định theo chuẩn chung của cả nước
     */
    'defaults' => [
        'dictionary' => 'standard',
    ],
    'dictionaries' => [
        /**
         * Cấu hình các từ điển custom theo ý bạn.
         */
        'standard' => PHPViet\NumberToWords\Dictionary::class,
        'south' => PHPViet\NumberToWords\SouthDictionary::class
    ]
];

Ngay bây giờ bạn hãy thử đổi default standard sang south, toàn bộ phương thức chuyển đổi số sang chữ số và tiền tệ sẽ đọc theo phong cách trong Nam:

use N2W;

// một trăm linh một => một trăm lẻ một
N2W::toWords(101);

// một nghìn => một ngàn
N2W::toWords(1000);

 // hai mươi tư => hai mươi bốn
N2W::toWords(24);

// một trăm hai mươi tư nghìn không trăm linh một đồng => một trăm hai mươi bốn ngàn không trăm lẻ một đồng
N2W::toCurrency(124001);

hoặc bạn muốn sử dụng linh động hơn thì hãy chỉ định từ điển:

// một trăm hai mươi tư nghìn không trăm linh một
n2w(124001);

// một trăm hai mươi bốn ngàn không trăm lẻ một
n2w(124001, 'south');

Nếu như bạn muốn thay đổi cách đọc theo ý bạn thì hãy tạo một lớp Dictionary kế thừa PHPViet\NumberToWords\Dictionary hoặc thực thi mẫu trừu tượng PHPViet\NumberToWords\DictionaryInterface:

use PHPViet\NumberToWords\Dictionary;
use PHPViet\NumberToWords\Transformer;

class MyDictionary extends Dictionary {

    /**
     * @inheritDoc
     */
    public function specialTripletUnitFive(): string
    {
        return 'nhăm';
    }

}

Sau đó khai báo vào config:

return [
    /**
     * Cấu hình từ điển mặc định theo chuẩn chung của cả nước
     */
    'defaults' => [
        'dictionary' => 'my',
    ],
    'dictionaries' => [
        /**
         * Cấu hình các từ điển custom theo ý bạn.
         */
        'standard' => PHPViet\NumberToWords\Dictionary::class,
        'south' => PHPViet\NumberToWords\SouthDictionary::class,
        'my' => MyDictionary::class
    ]
];

Và hãy thử ngay:

use N2W;

// mười nhăm
N2W::toWords(15);

Dành cho nhà phát triển

Nếu như bạn cảm thấy extension còn thiếu sót hoặc sai sót và bạn muốn đóng góp để phát triển chung, chúng tôi rất hoan nghênh! Hãy tạo các issue để đóng góp ý tưởng cho phiên bản kế tiếp hoặc tạo PR để đóng góp. Cảm ơn!

laravel-number-to-words's People

Contributors

nhamtphat avatar vuongxuongminh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-number-to-words's Issues

Không thể cài đặt trên Laravel 8

Khi mình cài đặt gói thông qua composer thì nhận được thông báo lỗi, có vẻ như có xung đột với gói

Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - phpviet/laravel-number-to-words[dev-master, 1.0.0] require illuminate/support ^5.7 -> found illuminate/support[v5.7.0, ..., 5.8.x-dev] but it conflicts with another require.
    - phpviet/laravel-number-to-words 1.0.x-dev is an alias of phpviet/laravel-number-to-words dev-master and thus requires it to be installed too.
    - Root composer.json requires phpviet/laravel-number-to-words ^1.0 -> satisfiable by phpviet/laravel-number-to-words[1.0.0, 1.0.x-dev (alias of dev-master)].


Installation failed, reverting ./composer.json and ./composer.lock to their original content.

Đọc nhầm cents tròn chục

n2c('1612.50', ['đô la Mỹ', 'cents']);
Mong đợi:
"Một nghìn sáu trăm mười hai đô la Mỹ năm mươi cents"

Kết quả:
"Một nghìn sáu trăm mười hai đô la Mỹ năm cents"

Kết quả của .10 .20 .30 ... đều là một cents, hai cents, cents thay vì mười, hai mươi, ba mươi cents

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.