Flutter IO Dev
Flutter IO Dev

Reputation: 2177

Show Numbers In Arabic Format Dart

How to show numbers in Arabic format in flutter app?

instead of showing the number as 1,2,3 I need to show them as ١, ٢, ٣

I have tried to use the NumberFormat class, but it doesn't work.

Flutter Doctor:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel dev, v0.8.2, on Microsoft Windows [Version 10.0.17134.345], locale ar-SA)
[√] Android toolchain - develop for Android devices (Android SDK 28.0.2)
[√] Android Studio (version 3.1)
[√] IntelliJ IDEA Community Edition (version 2018.2)
[!] VS Code, 64-bit edition (version 1.27.2)
[√] Connected devices (2 available)

! Doctor found issues in 1 category.

Example:

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'package:intl/intl.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      locale: Locale("ar"),
      home: new Scaffold(
        body: new Center(
          child: new Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(NumberFormat().format(1)),
              new Text(NumberFormat().format(2)),
              new Text(NumberFormat().format(3)),
            ],
          ),
        ),
      ),
    );
  }
}

Upvotes: 8

Views: 5656

Answers (4)

Radico
Radico

Reputation: 356

Using EG (Egypt) as the country code in intl package would solve the issue without needing to add a new font.

import 'package:intl/intl.dart';

void main() {
   print(NumberFormat('#.##', 'ar_EG').format(25.46));
}

Output: ٢٥٫٤٦

Upvotes: 2

teavun
teavun

Reputation: 51

String convertToArabicNumber(String number) {
  String res = '';

  final arabics = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
  number.characters.forEach((element) {
     res += arabics[int.parse(element)];
  });

/*   final latins = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; */
  return res;
}

Upvotes: 4

Fathah Cr
Fathah Cr

Reputation: 511

This package will do it for you arabic_numbers https://pub.dev/packages/arabic_numbers

Upvotes: 1

Flutter IO Dev
Flutter IO Dev

Reputation: 2177

Adding a font that supports Arabic numbers solve this issue.

Upvotes: 5

Related Questions