4

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)),
            ],
          ),
        ),
      ),
    );
  }
}

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Flutter IO Dev
  • 1,759
  • 5
  • 15
  • 20
  • Sounds like https://stackoverflow.com/questions/8487089/how-to-display-arabic-text-and-numbers-in-correct-format (Android native). Perhaps you need to specify a font that shows them as Arabic numbers. – Günter Zöchbauer Oct 17 '18 at 14:43
  • Where do you specify that the font should be used? https://flutter.io/cookbook/design/fonts/ – Günter Zöchbauer Oct 17 '18 at 15:12
  • 1
    @GünterZöchbauer, thanks for your replay. It's really not a flutter issue. I have tried to use a [font](https://fontzone.net/font-details/arabictwo-bold) that supports Arabic numbers and [added it to flutter](https://flutter.io/cookbook/design/fonts/) and it's work. – Flutter IO Dev Oct 17 '18 at 15:27
  • Glad to hear \o/ – Günter Zöchbauer Oct 17 '18 at 15:28
  • for me its not working. I tried to add google fonts that supports arabic number – shahana kareen Jul 13 '20 at 11:48
  • Not working for me either. The thing i don't understand is that DateFormat can convert date to Arabic numbers but NumberFormat doesn't. – Rafay Ali Jun 24 '21 at 08:47

4 Answers4

4

Adding a font that supports Arabic numbers solve this issue.

Flutter IO Dev
  • 1,759
  • 5
  • 15
  • 20
1
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;
}
teavun
  • 21
  • 3
0

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

Fathah Cr
  • 441
  • 1
  • 8
  • 17
0

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: ٢٥٫٤٦

Radico
  • 336
  • 1
  • 5
  • 19