number_display 1.0.1

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • new50

number_display #

Display number smartly within a certain length.

result = display(input, length, ...optionalConfigs)

To display data in a width-limited component, this function will smartly help you to convert number to a certain chart length. To be simple, plain, flexible and accurate, the conversion follow this rules:

  • result chart length will never overflow length
  • filter strange inputs ( null/NaN/object ) to placeholder ( -- )
  • use locale string with commas ( 1,234,222 ) as possible ( configurable )
  • use number with units ( 1.23k ) when length is limited
  • NO scientific notation ( 1.23e+4 )
  • return input text if allowed

Usage #

import 'package:number_display/number_display.dart';

// result = display(input, length, ...optionalConfigs)
// optionalConfigs: {maxAccuracy, placeholder, allowText, comma, units, unitInterval, unitMaxAccuracy}

display(1234.123)                   // 1,234.12
display(1234.123, 4)                // 1.2k
display('1000000000')               // 1G
display(-1.2345e+5)                 // -123,450
display(NaN)                        // --
display('')                         // --
display(new Date())                 // --
display((new Date()).toISOString()) // 2018-09-

display(NaN, 2, {placeholder: '**'})     // **
display(1.22, 8, {maxAccuracy: 0})       // 1.2
display('text', 8, {allowText: false})   // --
display(12345678, 19, {comma: false})    // 12345678

display(1234567867, 8, {unitsMaxAccuracy: 2})                           // 1.23G
display(12345, 4, {units: ['t', 'h', 'k', 'tk'], unitsInterval: 1})     // 1tk
display(123457777, 4, {units: ['t', 'h', 'k', 'tk'], unitsInterval: 1}) // --

Params #

input

The thing you want to display as a number. When it can't be converted to a number, the result would be a placeholder.

length

( default: 8 )

The max length the result would be. length should no less then 4 so that any number can display ( say -123 ) after trim.

optionalConfigs

  • maxAccuracy

    ( default: 4 )

    The max accuracy. The final accuracy will be calculated by length(of course less than maxAccuracy), so usually you don't need to set this param.

  • placeholder

    ( default: '--' )

    The result when the input can't be converted to a number. it's length should no more than your length param.

  • allowText

    ( default: true )

    Allow Text ( String that cant convert to number/null/undefined/NaN ) as input and result. If false , result of text will be placeholder.

  • comma

    ( default: true )

    Whether the locale string has commas ( 1,234,222 ).

  • units

    (defult: ['k', 'M', 'G', 'T'])

    Units for result number after compressed, start with lowest one, which is for 1*unitInterval compression. If number is greater than the highest unit can display , it will return placeholder.

  • unitInterval

    (defult: 3)

    The interval figures between each units. If units is defult ['k', 'M', 'G', 'T'], unitInterval should be defult 3.

  • unitMaxAccuracy

    (defult: 4)

    The max accuracy when displayed with units.

Change Log #

CHANGELOG.md

1.0.0 #

2019-02-07

  • Init this package.

1.0.1 #

2019-02-07

  • Add some documents.

example/main.dart

import 'package:number_display/number_display.dart';

main(List<String> args) {
  print(display(1234.123, 4));
  print(display(1.00, 8));
  print(display(1.12345678, 8));
  print(display(123, 8));
  print(display(1234, 8));
  print(display(123456789123, 8));
  print(display(12345.43212, 8));
  print(display(1.2345e+5, 8));
  print('---------------------------');
  print(display(-1, 8));
  print(display(-1.00, 8));
  print(display(-1.12345678, 8));
  print(display(-123, 8));
  print(display(-1234, 8));
  print(display(-123456789123, 8));
  print(display(-12345.43212, 8));
  print(display(-1.2345e+5, 8));
  print('---------------------------');
  print(display('-1234', 8));
  print(display('123456789123', 8));
  print(display('-1.2345e+5', 8));
  print('---------------------------');
  print(display(double.nan, 8));
  print(display(double.infinity, 8));
  print(display(double.negativeInfinity, 8));
  print(display(null, 8));
  print(display([], 8));
  print(display({}, 8));
  print(display(DateTime.now(), 8));
  print(display(DateTime.now().toString(), 8));
  print('---------------------------');
  print(display(1000000000, 4));
  print(display(1.00, 4));
  print(display(1.12345678, 4));
  print(display(123, 4));
  print(display(1234, 8));
  print(display(123456789123, 4));
  print(display(12345.43212, 4));
  print(display(1.2345e+5, 4));
  print('---------------------------');
  print(display(-1, 4));
  print(display(-1.00, 4));
  print(display(-1.12345678, 4));
  print(display(-123, 4));
  print(display(-1234, 4));
  print(display(-123456789123, 4));
  print(display(-12345.43212, 4));
  print(display(-1.2345e+5, 4));
  print('---------------------------');
  print(display(1.22, 8, maxAccuracy: 1));
  print(display('text', 8, allowText: false));
  print(display(12345678, 19, comma: true));
  print(display(12345678, 19, comma: false));
  print('---------------------------');
  print(display(1234567867, 8, units: ['w', 'y'], unitsInterval: 4));
  print(display(1234567867, 8, unitsMaxAccuracy: 2));
  print(display(12345, 4, units: ['t', 'h', 'k', 'tk'], unitsInterval: 1));
  print(display(123457777, 4, units: ['t', 'h', 'k', 'tk'], unitsInterval: 1));
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  number_display: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:number_display/number_display.dart';
  
Version Uploaded Documentation Archive
1.0.1 Feb 7, 2019 Go to the documentation of number_display 1.0.1 Download number_display 1.0.1 archive
1.0.0 Feb 7, 2019 Go to the documentation of number_display 1.0.0 Download number_display 1.0.0 archive
0.0.1 Feb 4, 2019 Go to the documentation of number_display 0.0.1 Download number_display 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
50
Learn more about scoring.

We analyzed this package on Feb 14, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.13

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:number_display/number_display.dart.

Health suggestions

Format lib/number_display.dart.

Run dartfmt to format lib/number_display.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0