1) { numberString = (number" name="description" /> 1) { numberString = (number" />
8091

How to go about formatting 1200 to 1.2k in Android studio

<h3>Question</h3>

I'd like to format following numbers into the numbers next to them with Android:

1000 to 1k 5821 to 5.8k 2000000 to 2m 7800000 to 7.8m


<h3>Answer1:</h3>

This should do the trick

String numberString = ""; if (Math.abs(number / 1000000) > 1) { numberString = (number / 1000000).toString() + "m"; } else if (Math.abs(number / 1000) > 1) { numberString = (number / 1000).toString() + "k"; } else { numberString = number.toString(); }
<h3>Answer2:</h3>

Try this trick:

private String numberCalculation(long number) { if (number < 1000) return "" + number; int exp = (int) (Math.log(number) / Math.log(1000)); return String.format("%.1f %c", number / Math.pow(1000, exp), "kMGTPE".charAt(exp-1)); }
<h3>Answer3:</h3>

Function

public String prettyCount(Number number) { char[] suffix = {' ', 'k', 'M', 'B', 'T', 'P', 'E'}; long numValue = number.longValue(); int value = (int) Math.floor(Math.log10(numValue)); int base = value / 3; if (value >= 3 && base < suffix.length) { return new DecimalFormat("#0.0").format(numValue / Math.pow(10, base * 3)) + suffix[base]; } else { return new DecimalFormat("#,##0").format(numValue); } }

Use

prettyCount(789); Output: 789 prettyCount(5821); Output: 5.8k prettyCount(101808); Output: 101.8k prettyCount(7898562); Output: 7.9M
<h3>Answer4:</h3>

Try this method :

For Java

public static String formatNumber(long count) { if (count < 1000) return "" + count; int exp = (int) (Math.log(count) / Math.log(1000)); return String.format("%.1f %c", count / Math.pow(1000, exp),"kMGTPE".charAt(exp-1)); }

For Kotlin(Android)

fun getFormatedNumber(count: Long): String { if (count < 1000) return "" + count val exp = (ln(count.toDouble()) / ln(1000.0)).toInt() return String.format("%.1f %c", count / 1000.0.pow(exp.toDouble()), "kMGTPE"[exp - 1]) }

来源:https://stackoverflow.com/questions/41859525/how-to-go-about-formatting-1200-to-1-2k-in-android-studio

Recommend

  • How to go about formatting 1200 to 1.2k in Android studio
  • Extracting specific cells from multiple Excel files and compile it into one Excel file
  • SyntaxError: invalid label using jQuery.ajax()
  • Sort a list from Arrays.asList() changes also the origin array?
  • IMAP access to mail office 365 with javamail
  • Python numpy.random.normal
  • Cannot create a deployment that requests more than 2Gi memory
  • Is there a consistent global FormatSettings variable availabe?
  • Asp.net “disable” authentication in development environment
  • groovy: Have a field name, need to set value and don't want to use switch
  • codeblocks and C undefined reference to getline
  • Finding a value in an array of arrays (similar to VLOOKUP function in Excel) in Swift
  • Show alternate categorical labels in axis in ggplot2
  • Role Count using Graph Api against a tenant
  • get all files in git diff in intellij
  • App crashes when switching to landscape layout
  • Validating a Firebase Key [duplicate]
  • Jekyll - How do I create pages in the root directory?
  • android : speech recognition what are the technologies available
  • Boolean filter using a timestamp value on a dataframe in Python
  • Java Collections.shuffle() weird behaviour [closed]
  • How to use array in autohotkey?
  • Ruby on Rails: Get mediaplayer information (iTunes, TRAKTOR, Cog; current song + playlist)
  • Generate and export point cloud from Project Tango
  • Year over Year Stats from a Crossfilter Dataset
  • Why is ordered choice in pyparsing failing for my use case?
  • JavaScript Regex to Match Boundaries of Words with diacritics
  • How to turn off notice reporting in xampp?
  • reshape/remould data frame to create normalized bar chart and pie chart
  • Google App Engine Datastore: Dealing with eventual consistency
  • Background transfer download task failed when app was closed
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Accessing Arguments, Workflow Variables from custom activities
  • How to use FirstOrDefault inside Include
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • PHP Permalinks.. how to change?
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Running R's aov() mixed effects model from Python using rpy2
  • ReferenceError: TextEncoder is not defined
  • JavaScript RegExp Replace