Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text format support for example 10.000 => 10k #7

Closed
RoyalCoder88 opened this issue Nov 17, 2022 · 4 comments
Closed

Text format support for example 10.000 => 10k #7

RoyalCoder88 opened this issue Nov 17, 2022 · 4 comments

Comments

@RoyalCoder88
Copy link

Hi @mingsnx,

Thanks for your great package, can you please add support for formatting the text numbers for example like this:

      int value = int.parse(count);

      if (value < 1000) {
        // less than a thousand
        return value.toStringAsFixed(0);
      } else if (value >= 1000 && value < 1000000) {
        // less than 1 million
        double result = value / 1000;
        return "${result.toStringAsFixed(2)} K";
      } else if (value >= 1000000 && value < (1000000 * 10 * 100)) {
        // less than 100 million
        double result = value / 1000000;
        return "${result.toStringAsFixed(2)} M";
      } else if (value >= (1000000 * 10 * 100) &&
          value < (1000000 * 10 * 100 * 100)) {
        // less than 100 billion
        double result = value / (1000000 * 10 * 100);
        return "${result.toStringAsFixed(2)} B";
      } else if (value >= (1000000 * 10 * 100 * 100) &&
          value < (1000000 * 10 * 100 * 100 * 100)) {
        // less than 100 trillion
        double result = value / (1000000 * 10 * 100 * 100);
        return "${result.toStringAsFixed(2)} T";
      } else {
        return count;
      }

or guide me please how to achieve this, thanks in advance!

@mingsnx
Copy link
Owner

mingsnx commented Dec 7, 2022

Hi,@RoyalCoder88
I'm sorry, I've been too busy lately
You can do this first,divide and conquer, first deal with numbers, then with units,

last, set value and suffix, invoke setstate({})

@RoyalCoder88
Copy link
Author

Thanks a lot @mingsnx I really understand and appreciate your time, and I will give it a try, and I will be appreciated it if you will post a short real example, thanks in advance!

@mingsnx
Copy link
Owner

mingsnx commented Dec 8, 2022

Hi, @RoyalCoder88
example

import 'dart:math';
import 'package:animated_digit/animated_digit.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int fractionDigits = 0;
  String suffix = "";
  late num value = 0;

  void _incrementCounter() {
    setState(() {
      format(Random().nextInt(4294967296));
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            AnimatedDigitWidget(
              value: value,
              suffix: suffix,
              fractionDigits: fractionDigits,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void format(val) {
    if (val < 1000) {
      value = val;
      fractionDigits = 0;
      suffix = "";
    } else if (val >= 1000 && val < 1000000) {
      // less than 1 million
      value = val / 1000;
      fractionDigits = 2;
      suffix = " K";
    } else if (val >= 1000000 && val < (1000000 * 10 * 100)) {
      // less than 100 million
      value = val / 1000000;
      fractionDigits = 2;
      suffix = " M";
    } else if (val >= (1000000 * 10 * 100) &&
        val < (1000000 * 10 * 100 * 100)) {
      // less than 100 billion
      value = val / (1000000 * 10 * 100);
      fractionDigits = 2;
      suffix = " G";
    } else if (val >= (1000000 * 10 * 100 * 100) &&
        val < (1000000 * 10 * 100 * 100 * 100)) {
      // less than 100 trillion
      value = val / (1000000 * 10 * 100 * 100);
      fractionDigits = 2;
      suffix = " T";
    } else {
      value = val;
      fractionDigits = 0;
      suffix = "";
    }
  }
}

@RoyalCoder88
Copy link
Author

Thanks a lot, @mingsnx works like a charm❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants