PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 13, 2022

[FIXED] how to format the count if it is too large in value in flutter?

 September 13, 2022     cross-platform, dart, flutter     No comments   

Issue

This is my buildCountText function in flutter, where i am passing label and count as my arguments, I am using this function to show my count of followers in an app, but I don't know how to format my count if it's value is large in thousands or millions,I don't want to write the whole figure if its in million , I just want to write it as 1M(for example). but I don't know how to format it in that way.

buildCountText(String label, int count) {
    return Container(
      padding: EdgeInsets.only(top: 5, bottom: 6),
      alignment: Alignment.centerLeft,
      child: RichText(
        text: TextSpan(
          //text: label,
          style: TextStyle(
              fontFamily: "OpenSans", color: Theme.of(context).primaryColor),
          children: <TextSpan>[
            TextSpan(
                text: label,
                style: TextStyle(
                    fontFamily: "OpenSans", fontWeight: FontWeight.w500)),
            TextSpan(
                text:_isLoading?" ...": ' $count', style: TextStyle(fontWeight: FontWeight.bold)),
          ],
        ),
      ),
    );
  }

Solution

Use number formatter. https://pub.dev/documentation/intl/latest/intl/NumberFormat-class.html

var f = NumberFormat.compact();
print(f.format(100000)); //<= prints 100K


Answered By - LonelyWolf
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing