Stay organized with collections
Save and categorize content based on your preferences.
class Formatter
Utility class to aid in formatting common values that are not covered by the java.util.Formatter
class in java.util
Summary
Public methods |
static String! |
Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
|
static String! |
Returns a string in the canonical IPv4 format ###.
|
static String! |
Like formatFileSize , but trying to generate shorter numbers (showing fewer digits of precision).
|
Public constructors
Public methods
static fun formatFileSize(
context: Context?,
sizeBytes: Long
): String!
Formats a content size to be in the form of bytes, kilobytes, megabytes, etc.
As of O, the prefixes are used in their standard meanings in the SI system, so kB = 1000 bytes, MB = 1,000,000 bytes, etc.
In android.os.Build.VERSION_CODES#N
and earlier, powers of 1024 are used instead, with KB = 1024 bytes, MB = 1,048,576 bytes, etc.
If the context has a right-to-left locale, the returned string is wrapped in bidi formatting characters to make sure it's displayed correctly if inserted inside a right-to-left string. (This is useful in cases where the unit strings, like "MB", are left-to-right, but the locale is right-to-left.)
Parameters |
context |
Context?: Context to use to load the localized units This value may be null . |
sizeBytes |
Long: size value to be formatted, in bytes |
Return |
String! |
formatted string with the number |
static fun formatIpAddress(ipv4Address: Int): String!
Deprecated: Use java.net.InetAddress#getHostAddress()
, which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses.
Returns a string in the canonical IPv4 format ###.###.###.### from a packed integer containing the IP address. The IPv4 address is expected to be in little-endian format (LSB first). That is, 0x01020304 will return "4.3.2.1".
static fun formatShortFileSize(
context: Context?,
sizeBytes: Long
): String!
Like formatFileSize
, but trying to generate shorter numbers (showing fewer digits of precision).
Parameters |
context |
Context?: This value may be null . |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Formatter\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nFormatter\n=========\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/android/text/format/Formatter \"View this page in Java\") \n\n```\nclass Formatter\n```\n\n|---|------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [android.text.format.Formatter](#) |\n\nUtility class to aid in formatting common values that are not covered by the [java.util.Formatter](../../../java/util/Formatter.html#) class in [java.util](../../../java/util/package-summary.html)\n\nSummary\n-------\n\n| Public constructors ||\n|--------------------------------------|---|\n| [Formatter](#Formatter())`()` \u003cbr /\u003e |\n\n| Public methods ||\n|------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| static [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | [formatFileSize](#formatFileSize(android.content.Context,%20kotlin.Long))`(`context:` `[Context](../../content/Context.html#)?`, `sizeBytes:` `[Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)`)` Formats a content size to be in the form of bytes, kilobytes, megabytes, etc. |\n| static [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | [formatIpAddress](#formatIpAddress(kotlin.Int))`(`ipv4Address:` `[Int](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-int/index.html)`)` Returns a string in the canonical IPv4 format ###. |\n| static [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | [formatShortFileSize](#formatShortFileSize(android.content.Context,%20kotlin.Long))`(`context:` `[Context](../../content/Context.html#)?`, `sizeBytes:` `[Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html)`)` Like [formatFileSize](#formatFileSize(android.content.Context,%20kotlin.Long)), but trying to generate shorter numbers (showing fewer digits of precision). |\n\nPublic constructors\n-------------------\n\n### Formatter\n\n```\nFormatter()\n```\n\nPublic methods\n--------------\n\n### formatFileSize\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic fun formatFileSize(\n context: Context?, \n sizeBytes: Long\n): String!\n```\n\nFormats a content size to be in the form of bytes, kilobytes, megabytes, etc.\n\nAs of O, the prefixes are used in their standard meanings in the SI system, so kB = 1000 bytes, MB = 1,000,000 bytes, etc.\n\nIn [android.os.Build.VERSION_CODES#N](../../os/Build.VERSION_CODES.html#N:kotlin.Int) and earlier, powers of 1024 are used instead, with KB = 1024 bytes, MB = 1,048,576 bytes, etc.\n\nIf the context has a right-to-left locale, the returned string is wrapped in bidi formatting characters to make sure it's displayed correctly if inserted inside a right-to-left string. (This is useful in cases where the unit strings, like \"MB\", are left-to-right, but the locale is right-to-left.)\n\n| Parameters ||\n|-------------|--------------------------------------------------------------------------------------------------------------------|\n| `context` | [Context](../../content/Context.html#)?: Context to use to load the localized units This value may be `null`. |\n| `sizeBytes` | [Long](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-long/index.html): size value to be formatted, in bytes |\n\n| Return ||\n|-----------------------------------------------------------------------------------|----------------------------------|\n| [String](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html)! | formatted string with the number |\n\n### formatIpAddress\n\nAdded in [API level 3](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \nDeprecated in [API level 15](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic fun formatIpAddress(ipv4Address: Int): String!\n```\n\n**Deprecated:** *Use [java.net.InetAddress#getHostAddress()](../../../java/net/InetAddress.html#getHostAddress()), which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses.*\n\nReturns a string in the canonical IPv4 format ###.###.###.### from a packed integer containing the IP address. The IPv4 address is expected to be in little-endian format (LSB first). That is, 0x01020304 will return \"4.3.2.1\". \n\n### formatShortFileSize\n\nAdded in [API level 5](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nstatic fun formatShortFileSize(\n context: Context?, \n sizeBytes: Long\n): String!\n```\n\nLike [formatFileSize](#formatFileSize(android.content.Context,%20kotlin.Long)), but trying to generate shorter numbers (showing fewer digits of precision).\n\n| Parameters ||\n|-----------|--------------------------------------------------------------------|\n| `context` | [Context](../../content/Context.html#)?: This value may be `null`. |"]]