FVod
FVod

Reputation: 2295

How to know whether use metric or imperial

How could I know in android whether to use metric or imperial? I haven't seen any option in Locale, and the only thing that comes to my mind is to use the locale.getCountry(); method and check whether the country is UK, US, ... But, is there an android's method to know it?

Thanks in advance!

Upvotes: 5

Views: 3074

Answers (2)

Codelaby
Codelaby

Reputation: 2873

I use this extension function for Kotlin:

fun Locale.isMetric(): Boolean {
    return when (country.uppercase(this)) {
        "US", "LR", "MM", "BS", "BZ", "KY", "PW", "GB", "UK" -> false
        else -> true
    }
}

Upvotes: 2

joecks
joecks

Reputation: 4637

A more or less complete way to do this is this way:

Kotlin:

private fun Locale.toUnitSystem() =
    when (country.toUpperCase()) {
        // https://en.wikipedia.org/wiki/United_States_customary_units
        // https://en.wikipedia.org/wiki/Imperial_units
        "US" -> UnitSystem.IMPERIAL_US
        // UK, Myanmar, Liberia, 
        "GB", "MM", "LR" -> UnitSystem.IMPERIAL
        else -> UnitSystem.METRIC
    }

Note that there is a difference between UK and US imperial systems, see the wiki articles for more details.

Upvotes: 2

Related Questions