Issue
My result should look like this: $100 00,99
I managed to format the number as I need, but without the currency.
I managed to get the currency separately, but can't get the two together.
For the numbering format, I used DecimalFormatSymbol as in the answer to this question.
 private fun formatValue(value: Double, formatString: String): String {
     val formatSymbols = DecimalFormatSymbols(Locale.ENGLISH)
     formatSymbols.decimalSeparator = ','
     formatSymbols.groupingSeparator = ' '
     val formatter = DecimalFormat(formatString, formatSymbols)
     return formatter.format(value)
 }
 formatValue(amount ,"###,###.00")
For the currency I used this code:
fun getFormattedCurrency(currency: String, amount: Double): String {
     val c = Currency.getInstance(currency)
     val nf = NumberFormat.getCurrencyInstance()
     nf.currency = c
     return  nf.format(amount)
}
How can I combine the two?
Solution
Hope this helps you.
    val decimalFormatSymbols = DecimalFormatSymbols().apply {
        decimalSeparator = ','
        groupingSeparator = ' '
        setCurrency(Currency.getInstance("AED"))
    }
    val decimalFormat = DecimalFormat("$ #,###.00", decimalFormatSymbols)
    val text = decimalFormat.format(2333222)
    println(text) //$ 2 333 222,00
    val decimalFormat2 = DecimalFormat("¤ #,###.00", decimalFormatSymbols)
    val text2 = decimalFormat2.format(2333222)
    println(text2) //AED 2 333 222.00
Notifiy that if you use ¤ instead of specific currency symbol like $,€ it will use a symbol according to currency instance you create. Also you can get more info from docs. https://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
Also you can find ISO 4217 codes in https://en.wikipedia.org/wiki/ISO_4217
Answered By - toffor Answer Checked By - Willingham (PHPFixing Volunteer)
 
 Posts
Posts
 
 
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.