- How do I use kwn/number-to-words to convert a number to words in Laravel?
- Install it with composer require kwn/number-to-words, then create a NumberToWords instance and call getNumberTransformer for the RFC 3066 locale you need. Pass the value to toWords, so 5120 becomes five thousand one hundred twenty. In Laravel, bind the manager or your own wrapper class in a service provider so controllers and Blade views resolve it from the container. There is no database, queue, or cache requirement, so setup is essentially the Composer package plus an optional binding.
- Is kwn/number-to-words compatible with Laravel 10, 11 and 12?
- Yes, for the PHP versions those releases require. The 2.x line requires PHP 7.4 or newer, which sits below the PHP 8.0 floor of Laravel 9 and later, so the library is not the constraint. Because it is framework-agnostic, it also works on Laravel 8 and in plain PHP scripts outside Laravel. Check your PHP version rather than your Laravel version when evaluating compatibility.
- How should I register kwn/number-to-words in a Laravel service provider?
- The simplest approach is a singleton binding for the NumberToWords manager, resolved wherever you need a transformer. A cleaner pattern is to bind a small application-owned class such as AmountSpeller that instantiates NumberToWords internally and resolves transformers from your app's active locale. Feature code then depends on your wrapper rather than the package, which keeps the library swappable and gives one place for locale fallbacks and minor-unit normalization. A facade is optional and mainly useful for Blade templates that render spelled-out amounts.
- Why is the currency transformer returning the wrong amount for decimals?
- The currency transformer accepts integers only, so you must pass whole minor units. For example, multiply a 51.20 amount by 100 and pass 5120 together with the ISO 4217 currency code. Doing this at your integration boundary prevents float rounding from silently producing incorrect words. Keep money in integer cents throughout and convert to words only at the final rendering step.
- Which languages and currencies does kwn/number-to-words support?
- The README support matrix lists the available number and currency transformers per locale, identified by RFC 3066 language codes. Coverage is asymmetric: several languages, including Czech, Dutch, Italian, Persian and Swedish, support numbers but not currency. If your product promises localized currency amounts across every market, map your roadmap locales against that matrix and plan a fallback for gaps. Verify the matrix for your exact version rather than assuming all locales are equivalent.
- Can I use kwn/number-to-words for invoices and legally binding spelled-out amounts?
- It is a community-maintained dictionary library, not a certified financial or legal component, so treat mistranslation as a business-correctness risk. Edge cases such as zero, negative values, very large numbers and uncommon currencies are not guaranteed, and the release history shows a steady stream of locale-specific dictionary fixes. If spelled-out amounts carry contractual or regulatory exposure, build regression tests for every locale and currency combination you use and have native speakers review sample output. Consider a documented fallback, such as numeric-only rendering, when a locale transformer is missing or suspect.
- How do I test number-to-words output in Laravel feature tests?
- Test through your own wrapper service rather than the package internals so assertions stay stable if the library is replaced. Use PHPUnit data providers keyed by locale, currency and amount, and assert the exact expected string for representative values including zero, negatives, decimals expressed in minor units, and large numbers. Add a test that fails if a roadmap locale lacks a currency transformer. Because translation dictionaries are community-sourced, treat these tests as regression protection for upgrades.
- Should I call NumberToWords::transformNumber directly or inject a transformer?
- The static helpers are convenient for quick scripts, but they are hard to mock and couple your feature code to the package. Injecting the manager or, better, an application-owned wrapper keeps calls testable and makes locale selection, fallback behavior and minor-unit handling explicit. It also means a future swap to another library only touches one class. Use static calls only in throwaway code or Blade helpers where a container binding is impractical.
- What are the alternatives to kwn/number-to-words for spelling out amounts in PHP?
- PHP's intl extension does not spell out amounts, so alternatives are other Composer number-to-words libraries, locale-specific helper packages, or writing your own formatter for a small set of languages. Compare them on the locale and currency pairs you actually need, on integer-only versus decimal handling, and on whether their dictionaries are tested. For a narrow single-locale requirement, a custom formatter can be less risk than a broad library with uneven coverage. kwn/number-to-words is a reasonable default when its support matrix covers your roadmap.
- Is kwn/number-to-words actively maintained and safe for production?
- It has an active release cadence, CI, coverage and maintainability badges, and an MIT license, which are good signs. Adoption is niche, with roughly 427 GitHub stars and few reported dependents, so you are effectively the primary QA for your locale and currency combinations. Pin the version in composer.json, review changelogs for dictionary changes, and verify the latest release on Packagist before planning upgrades, because some repository metadata dates are unreliable. For most applications the technical risk is low while the translation-correctness risk is moderate.