num2persian
num2persian
is a Dart library for converting numbers to their Persian word equivalents. This can be useful for displaying numbers in a more readable format in Persian-language applications, such as writing out monetary amounts in words.
Features
- Convert numbers to Persian words.
- Convert numbers to Persian monetary units (Toman, Rial).
- Format numbers with commas.
- Convert numbers to Persian numerals.
- Convert Rial to Toman and vice versa.
- Support for both
int
andString
types.
Installation
Add the following dependency to your pubspec.yaml
file:
dependencies:
num2persian: ^0.0.2
Then, run flutter pub get
to fetch the package.
Usage
To use the num2persian
library, import it in your Dart code:
import 'package:num2persian/num2persian.dart';
Examples
Convert Number to Persian Words
For String
type:
String number = "123";
print(number.toPersianLetter()); // Output: یکصد و بیست و سه
For int
type:
int number = 123;
print(number.toPersianLetter()); // Output: یکصد و بیست و سه
Convert Number to Persian Monetary Units
To Toman:
String number = "12345";
print(number.toToman()); // Output: دوازده هزار و سیصد و چهل و پنج تومان
To Rial:
int number = 12345;
print(number.toRial()); // Output: دوازده هزار و سیصد و چهل و پنج ریال
Format Number with Commas
For String
type:
String number = "1234567";
print(number.formatWithCommas()); // Output: 1,234,567
For int
type:
int number = 1234567;
print(number.formatWithCommas()); // Output: 1,234,567
Convert Number to Persian Numerals
For String
type:
String number = "1234567";
print(number.toPersianNumber()); // Output: ۱۲۳۴۵۶۷
For int
type:
int number = 1234567;
print(number.toPersianNumberInt()); // Output: ۱۲۳۴۵۶۷
Convert Rial to Toman
For String
type:
String rial = "1234567";
print(rial.rialToToman()); // Output: 123456
For int
type:
int rial = 1234567;
print(rial.rialToToman()); // Output: 123456
Convert Toman to Rial
For String
type:
String toman = "123456";
print(toman.tomanToRial()); // Output: 1234560
For int
type:
int toman = 123456;
print(toman.tomanToRial()); // Output: 1234560
Flutter Usage
You can use num2persian
in your Flutter applications to display numbers in Persian words within your UI components.
Example Flutter App
Here's a simple example of a Flutter app using num2persian
:
import 'package:flutter/material.dart';
import 'package:num2persian/num2persian.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('num2persian Example'),
),
body: Center(
child: NumberDisplay(),
),
),
);
}
}
class NumberDisplay extends StatelessWidget {
@override
Widget build(BuildContext context) {
String number = "123456";
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Number: $number'),
Text('In Persian: ${number.toPersianLetter()}'),
Text('As Toman: ${number.toToman()}'),
Text('As Rial: ${number.toRial()}'),
Text('With Commas: ${number.formatWithCommas()}'),
Text('In Persian Numerals: ${number.toPersianNumber()}'),
Text('Rial to Toman: ${number.rialToToman()}'),
Text('Toman to Rial: ${number.tomanToRial()}'),
],
);
}
}
This example creates a simple Flutter app with a NumberDisplay
widget that shows the number in various formats using the num2persian
library.
API
Extensions on String
String toPersianLetter()
: Converts the numeric string to its Persian word equivalent.String toToman()
: Converts the numeric string to Persian words followed by "تومان".String toRial()
: Converts the numeric string to Persian words followed by "ریال".String toPersianNumber()
: Converts the numeric string to Persian numerals.String formatWithCommas()
: Formats the numeric string with commas.String rialToToman()
: Converts the numeric string from Rial to Toman.String tomanToRial()
: Converts the numeric string from Toman to Rial.
Extensions on int
String toPersianLetter()
: Converts the integer to its Persian word equivalent.String toToman()
: Converts the integer to Persian words followed by "تومان".String toRial()
: Converts the integer to Persian words followed by "ریال".String toPersianNumberInt()
: Converts the integer to Persian numerals.String formatWithCommas()
: Formats the integer with commas.String rialToToman()
: Converts the integer from Rial to Toman.String tomanToRial()
: Converts the integer from Toman to Rial.
Contributing
Contributions are welcome! Please submit a pull request or create an issue for any bugs or feature requests.
License
This project is licensed under the Apache License. See the LICENSE file for details.