Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I format a number before it is rendered in <Text>

Tags:

flutter

dart

I am rendering a <Text> node in Flutter app something like:

We have total ${_summary['bookCount']} books. 

_summary is retrieved via a remote API and bookCount is one of the returned JSON field. It normally is more than 1,000.

If I display the book count like that, it is a plain 1234. I would like to make it be shown as 1,234.

Currently, I have to manually modify that field using some formatter but this is cumbersome.

I am looking for something like:

We have total ${myNumberFormat(_summary['bookCount'])} books. 

grammar, where myNumberFormat is a function.

In my previous programming in PHP and Twig, this can be done with a filter.

Much appreciate your input.

Update

@raju-bitter

This solution is what I know and is absolutely correct. What I am looking for is an inline "filter".

With this solution, a few things I don't like, the most of which is that, I have to split my one liner of text into several few segments: We have {XXX} books, accumulating to {YYY} pages, and {ZZZ} word counts. This sentence will be broken to 7 parts at least so that each number text section can be formatted via a formatter and then wrapped in a surrounding <Text>.

I am trying to see if there are more straightforward ways to do so.

like image 421
TaylorR Avatar asked Apr 26 '17 12:04

TaylorR


People also ask

How do you format a float?

format("%. 2f", 1.23456); This will format the floating point number 1.23456 up-to 2 decimal places, because we have used two after decimal point in formatting instruction %.

How do you format a number to currency when using react?

To format a number to currency when using React Native, we can use the react-number-format package. To install it, we run npm i react-number-format . to add the NumberFormat component. value is set to the number to format.


3 Answers

There is a Dart package for formatting numbers, the Dart intl package. To use the package, add the following line to the Dart dependencies: pubspec.yaml file:

  intl: ^0.17.0

Here's what my dependencies look like with the line:

dependencies:
  flutter:
    sdk: flutter
    intl: ^0.17.0

Click packages get in IntelliJ, or run flutter packages get from the command line.

Make sure your class imports the intl package:

import 'package:intl/intl.dart' as intl;

In your code, you can use NumberFormat class to do the formatting:

  final formatter =  intl.NumberFormat.decimalPattern().format(1234) // formatted number will be: 1,234

Full stateful widget example:

class NumberFormatExample extends StatefulWidget {
  @override
  _NumberFormatExampleState createState() => new _NumberFormatExampleState();
}

class _NumberFormatExampleState extends State<NumberFormatExample> {
  final formatter = intl.NumberFormat.decimalPattern();
  int theValue = 1234;

  @override
  Widget build(BuildContext context) {
    return new Text(formatter.format(theValue));
  }
}
  
like image 158
raju-bitter Avatar answered Oct 03 '22 23:10

raju-bitter


An update for above answers:

First add intl package into your pubspec.yaml file, just after flutter sdk (like below):

    dependencies:
      flutter:
        sdk: flutter
      intl: ^0.16.0

If you use flutter_localizations, intl must be above of that.

Now you can use NumberFormat class.

Some examples:

print(NumberFormat.currency().format(123456)); // USD123,456.00

print(NumberFormat.currency(locale: 'eu').format(123456)); // 123.456,00 EUR

print(NumberFormat.currency(name: 'EURO').format(123456)); // EURO123,456.00

print(NumberFormat.currency(locale: 'eu', symbol: '?').format(123456)); // 123.456,00 ?

print(NumberFormat.currency(locale: 'eu', decimalDigits: 3).format(123456)); // 123.456,000 EUR

print(NumberFormat.currency(locale: 'eu', customPattern: '\u00a4 #,##.#').format(123456)); // EUR 12.34.56,00
  • Refrence & More information: https://www.woolha.com/tutorials/dart-formatting-currency-with-numberformat#supported-locales

  • NumberFormat Class Dart API: https://api.flutter.dev/flutter/intl/NumberFormat-class.html

like image 42
Ali Radmanesh Avatar answered Oct 03 '22 23:10

Ali Radmanesh


Using intl package,

double number = 1234;
String output = NumberFormat.decimalPattern().format(number); // 1,234
like image 3
CopsOnRoad Avatar answered Oct 04 '22 00:10

CopsOnRoad