Issue
I want to use a more than 1 data type inside a placeholder within a String resource but whenever I try to use a demical number as a numerical value, an error is returned.
Kotlin
val currentLocale = Locale.getDefault()
val distanceWalked = 5.2
val numberFormatter: NumberFormat
val amountOut: String
numberFormatter = NumberFormat.getNumberInstance(currentLocale)
amountOut = numberFormatter.format(distanceWalked)
tv.txt = getString(R.string.distance_decimalnumber_placeholder,amountOut,"*")
Stirng resouce
<string name="distance_decimalnumber_placeholder">You have walked %1$d%2$s metres</string>
Expected result
You have walked 5.2* metres
Error
java.util.IllegalFormatConversionException: d != java.lang.Double
Wrong argument type for formatting argument '#1' in distance_decimalnumber_placeholder: conversion is 'd', received String (argument #2 in method call)
Solution
First mistake d
is for ints
The problem:
You aready converted to string at: amountOut = numberFormatter.format(distanceWalked)
you need to replace with string now
Solution: You don`t need to use $f
you need to use $s
tv.txt = getString(R.string.distance_decimalnumber_placeholder,amountOut /* this is a string also*/ ,"*")
<string name="distance_decimalnumber_placeholder">You have walked %1$s%2$s metres</string>
Answered By - Marcos Vasconcelos
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.