Latest 1.1.0
Homepage https://github.com/RenGate/MetricPrefixNumberFormatter
License MIT
Platforms ios 8.0
Authors

MetricPrefixNumberFormatter is an NSNumberFormatter subclass designed to format large and small numbers using the metric prefixes.
This is a must have library, if your app displays potentially big numbers like a number of views, likes, etc, or deals with calculations that may potentially involve very small numbers.

1200 -> 1.2 k
12300000 -> 12.3 M
0.0017 -> 1.7 m

Main features

  • Supports all metric prefixes: from yotta (10^24) to yocto (10^-24).
  • Localizable using the localizationDictionary property.
  • Configurable units and delimiters.

Examples

// 1122300 -> 1.12 M
let nf = MetricPrefixNumberFormatter()
nf.minimumFractionDigits = 2
nf.stringWithMetricPrefix(from: 1122300)
// 0.001 -> 1 mA
let nf = MetricPrefixNumberFormatter()
nf.unit = "A"
nf.stringWithMetricPrefix(from: 0.001)
// 1200 -> 1.2KV
let nf = MetricPrefixNumberFormatter()
nf.unit = "V"
nf.delimiter = ""
nf.stringWithMetricPrefix(from: 1200)

Since MetricPrefixNumberFormatter is a subclass of NSNumberFormatter its output can be configured the same way as with NSNumberFormatter. A good overview of NSNumberFormatter output setup using the minimum/maximumSignificantDigits, minimum/maximumFractionDigits, and usesSignificantDigits properties is presented in this guide by @samwize.

You can also check out provided example iOS app that shows you how various numbers are formatted by MetricPrefixNumberFormatter.

$ pod try MetricPrefixNumberFormatter

Requirements

  • iOS 8+
  • Xcode 10 / Swift 4.2 and higher

Installation using CocoaPods

pod 'MetricPrefixNumberFormatter', '~> 1.0.0'

License

MetricPrefixNumberFormatter is available under the MIT license. See LICENSE file for more information.

Latest podspec

{
    "name": "MetricPrefixNumberFormatter",
    "version": "1.1.0",
    "summary": "NSNumberFormatter subclass designed to format large and small numbers using the metric prefixes.",
    "homepage": "https://github.com/RenGate/MetricPrefixNumberFormatter",
    "license": "MIT",
    "authors": {
        "Rostyslav Dovhaliuk": "[email protected]"
    },
    "social_media_url": "https://twitter.com/rostyslav_d",
    "platforms": {
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/RenGate/MetricPrefixNumberFormatter.git",
        "tag": "1.1.0"
    },
    "source_files": "Source/*.{swift}",
    "swift_version": "5.0",
    "ios": {
        "frameworks": "Foundation"
    }
}

Pin It on Pinterest

Share This