num2text 0.0.4

SDKflutter
Platformandroidioswindowslinuxmacosweb

Number-to-words in 69 languages for Dart. Supports int, double, BigInt, Decimal, and numeric strings. Includes currency, year formatting, and locale-specific options.

Num2Text 0.0.4

pub package License: MIT

Dart library for converting numbers (integers, doubles, BigInt, Decimal, Numeric String) into their word representations (cardinal form) across a wide range of languages.

Currently supports up to 69 languages!

Features

  • Multi-Language Support: Convert numbers to words in 69 languages (see list below).
  • Flexible Input: Handles int, double, BigInt, String (if parsable), and Decimal types.
  • Large Numbers: Handles large integer parts up to 24 digits (up to sextillions in the English short scale). Support may vary slightly by language implementation.
  • Rich Formatting Options:
    • Currency formatting with language-specific unit names and plurals ([CurrencyInfo]).
    • Year formatting (e.g., "nineteen eighty-four", "2024 AD/BC").
    • Decimal handling (e.g., "one point five", "dix virgule cinq").
    • Negative number prefixes.
    • Language-specific grammatical features (gender, case, specific connectors like 'and'/'le').
  • Error Handling: Optional fallback for invalid inputs.
  • Well-Tested: Each language has dedicated tests (see Testing section).

Installation

Add this to your pubspec.yaml dependencies:

dependencies:
  num2text: ^0.0.3 # Use the latest version
  decimal: ^2.3.3 # Optional dependency

Then run dart pub get or flutter pub get.

Basic Usage

Import the library:

import 'package:num2text/num2text.dart';

Create an instance and convert:

// 1. Create an instance (defaults to English)
final num2text = Num2Text(); // Or Num2Text(initialLang: Lang.EN)

// 2. Basic conversion (English)
print(num2text.convert(123));           // Output: one hundred twenty-three
print(num2text.convert(1045.67));        // Output: one thousand forty-five point six seven
print(num2text(1000000));               // Callable instance: one million

// 3. Change language
num2text.setLang(Lang.VI); // Switch to Vietnamese
print(num2text.convert(987));           // Output: chín trăm tám mươi bảy

num2text.setLang(Lang.ES); // Switch to Spanish
print(num2text.convert(25));            // Output: veinticinco

Using Options

Customize output with language-specific options:

// English with British 'and' and GBP currency
num2text.setLang(Lang.EN);
print(num2text.convert(123, options: EnOptions(includeAnd: true)));
// Output: one hundred and twenty-three

print(num2text.convert(135.75, options: EnOptions(
  currency: true,
  currencyInfo: CurrencyInfo.gbp, // Use British Pound info
  includeAnd: true,
)));
// Output: one hundred and thirty-five pounds and seventy-five pence

// Vietnamese with 'lẻ' and year format
num2text.setLang(Lang.VI);
print(num2text.convert(105, options: ViOptions(useLe: true)));
// Output: một trăm lẻ năm
print(num2text(2024, options: ViOptions(format: Format.year)));
// Output: hai nghìn không trăm hai mươi tư

// Spanish with Euro currency
num2text.setLang(Lang.ES);
print(num2text(1.50, options: EsOptions(
  currency: true,
  currencyInfo: CurrencyInfo.eurEs, // Use Euro (Spanish) info
)));
// Output: un euro con cincuenta céntimos

// Polish with Złoty currency (demonstrates complex plurals)
num2text.setLang(Lang.PL);
print(num2text(123.45, options: PlOptions(currency: true))); // Uses PLN by default
// Output: sto dwadzieścia trzy złote i czterdzieści pięć groszy
print(num2text(5, options: PlOptions(currency: true)));
// Output: pięć złotych

Supported Languages

The library currently supports the following 69 languages. Each language has specific options available (e.g., EnOptions, ViOptions) and is tested individually. For complete usage examples of a specific language, including all options, please check the corresponding test file listed in the table.

LanguageEnum Value (Lang.XX)Test File Location
AfrikaansLang.AFtest/lang/lang_af_test.dart
AmharicLang.AMtest/lang/lang_am_test.dart
ArabicLang.ARtest/lang/lang_ar_test.dart
AzerbaijaniLang.AZtest/lang/lang_az_test.dart
BelarusianLang.BEtest/lang/lang_be_test.dart
BulgarianLang.BGtest/lang/lang_bg_test.dart
BengaliLang.BNtest/lang/lang_bn_test.dart
BosnianLang.BStest/lang/lang_bs_test.dart
CzechLang.CStest/lang/lang_cs_test.dart
DanishLang.DAtest/lang/lang_da_test.dart
GermanLang.DEtest/lang/lang_de_test.dart
GreekLang.ELtest/lang/lang_el_test.dart
EnglishLang.ENtest/lang/lang_en_test.dart
SpanishLang.EStest/lang/lang_es_test.dart
Persian (Farsi)Lang.FAtest/lang/lang_fa_test.dart
FinnishLang.FItest/lang/lang_fi_test.dart
FilipinoLang.FILtest/lang/lang_fil_test.dart
FrenchLang.FRtest/lang/lang_fr_test.dart
HausaLang.HAtest/lang/lang_ha_test.dart
HebrewLang.HEtest/lang/lang_he_test.dart
HindiLang.HItest/lang/lang_hi_test.dart
CroatianLang.HRtest/lang/lang_hr_test.dart
HungarianLang.HUtest/lang/lang_hu_test.dart
ArmenianLang.HYtest/lang/lang_hy_test.dart
IndonesianLang.IDtest/lang/lang_id_test.dart
IgboLang.IGtest/lang/lang_ig_test.dart
IcelandicLang.IStest/lang/lang_is_test.dart
ItalianLang.ITtest/lang/lang_it_test.dart
JapaneseLang.JAtest/lang/lang_ja_test.dart
GeorgianLang.KAtest/lang/lang_ka_test.dart
KazakhLang.KKtest/lang/lang_kk_test.dart
KhmerLang.KMtest/lang/lang_km_test.dart
KoreanLang.KOtest/lang/lang_ko_test.dart
KyrgyzLang.KYtest/lang/lang_ky_test.dart
LaoLang.LOtest/lang/lang_lo_test.dart
LithuanianLang.LTtest/lang/lang_lt_test.dart
LatvianLang.LVtest/lang/lang_lv_test.dart
MacedonianLang.MKtest/lang/lang_mk_test.dart
MongolianLang.MNtest/lang/lang_mn_test.dart
MalayLang.MStest/lang/lang_ms_test.dart
MalteseLang.MTtest/lang/lang_mt_test.dart
BurmeseLang.MYtest/lang/lang_my_test.dart
NepaliLang.NEtest/lang/lang_ne_test.dart
DutchLang.NLtest/lang/lang_nl_test.dart
NorwegianLang.NOtest/lang/lang_no_test.dart
PolishLang.PLtest/lang/lang_pl_test.dart
PortugueseLang.PTtest/lang/lang_pt_test.dart
RomanianLang.ROtest/lang/lang_ro_test.dart
RussianLang.RUtest/lang/lang_ru_test.dart
SinhalaLang.SItest/lang/lang_si_test.dart
SlovakLang.SKtest/lang/lang_sk_test.dart
SlovenianLang.SLtest/lang/lang_sl_test.dart
AlbanianLang.SQtest/lang/lang_sq_test.dart
SerbianLang.SRtest/lang/lang_sr_test.dart
SwedishLang.SVtest/lang/lang_sv_test.dart
SwahiliLang.SWtest/lang/lang_sw_test.dart
TamilLang.TAtest/lang/lang_ta_test.dart
TajikLang.TGtest/lang/lang_tg_test.dart
ThaiLang.THtest/lang/lang_th_test.dart
TurkmenLang.TKtest/lang/lang_tk_test.dart
TurkishLang.TRtest/lang/lang_tr_test.dart
UkrainianLang.UKtest/lang/lang_uk_test.dart
UrduLang.URtest/lang/lang_ur_test.dart
UzbekLang.UZtest/lang/lang_uz_test.dart
VietnameseLang.VItest/lang/lang_vi_test.dart
XhosaLang.XHtest/lang/lang_xh_test.dart
YorubaLang.YOtest/lang/lang_yo_test.dart
ChineseLang.ZHtest/lang/lang_zh_test.dart
ZuluLang.ZUtest/lang/lang_zu_test.dart

Testing

The library includes comprehensive tests for each supported language. You can find the language-specific tests under the test/lang/ directory, following the pattern lang_xx_test.dart (e.g., test/lang/lang_en_test.dart, test/lang/lang_vi_test.dart).

API Documentation

Full API documentation is available on pub.dev once published.

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests on GitHub.