-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
72 changed files
with
1,352 additions
and
529 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../strings/src/androidMain/kotlin/app/deckbox/common/resources/strings/CurrencyFormatter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package app.deckbox.common.resources.strings | ||
|
||
import android.icu.number.Notation | ||
import android.icu.number.NumberFormatter | ||
import android.icu.number.Precision | ||
import android.icu.text.NumberFormat | ||
import android.icu.util.Currency | ||
import android.os.Build | ||
import androidx.annotation.RequiresApi | ||
import java.util.Locale | ||
|
||
actual object CurrencyFormatter { | ||
|
||
actual fun format(value: Double, type: CurrencyType): String { | ||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { | ||
formatAndroid30(value, type) | ||
} else { | ||
formatLegacy(value, type) | ||
} | ||
} | ||
|
||
@RequiresApi(Build.VERSION_CODES.R) | ||
private fun formatAndroid30(value: Double, type: CurrencyType): String { | ||
return NumberFormatter.withLocale(Locale.getDefault()) | ||
.notation(Notation.compactLong()) | ||
.unit(type.asCurrency()) | ||
.precision(Precision.maxSignificantDigits(2)) | ||
.format(value) | ||
.toString() | ||
} | ||
|
||
private fun formatLegacy(value: Double, type: CurrencyType): String { | ||
return NumberFormat.getInstance(Locale.getDefault()) | ||
.apply { | ||
currency = type.asCurrency() | ||
maximumFractionDigits = 2 | ||
} | ||
.format(value) | ||
} | ||
|
||
private fun CurrencyType.asCurrency(): Currency { | ||
return when (this) { | ||
CurrencyType.USD -> Currency.getInstance(Locale.US) | ||
CurrencyType.EUR -> Currency.getInstance("EUR") | ||
} | ||
} | ||
} |
Oops, something went wrong.