@@ -173,26 +173,30 @@ data class ParsedState internal constructor(
173173 val unit : String? = null ,
174174 val format : String? = null
175175 ) : Parcelable {
176- override fun toString () = toString(Locale .getDefault())
176+ override fun toString () = toString(Locale .getDefault(), false )
177177
178178 /* *
179179 * Like [toString][.toString], but using a specific locale for formatting.
180180 */
181- fun toString (locale : Locale ): String {
181+ fun toString (locale : Locale , stripUnit : Boolean ): String {
182182 if (! format.isNullOrEmpty()) {
183- val actualFormat = format
184- .replace(" %unit%" , unit.orEmpty())
185- // In case of 'one' unit, the unit is part of the format pattern, but not part of the value
186- // sent by the server. Avoid ending the value with a space in that case.
187- .trim()
183+ val actualFormat = if (stripUnit) {
184+ format.split(" " ).first()
185+ } else {
186+ format
187+ .replace(" %unit%" , unit.orEmpty())
188+ // In case of 'one' unit, the unit is part of the format pattern, but not part of the value
189+ // sent by the server. Avoid ending the value with a space in that case.
190+ .trim()
191+ }
188192 try {
189193 return String .format(locale, actualFormat, getActualValue())
190194 } catch (e: IllegalFormatException ) {
191195 // State format pattern doesn't match the actual data type
192196 // -> ignore and fall back to our own formatting
193197 }
194198 }
195- return if (unit == null ) formatValue() else " ${formatValue()} $unit "
199+ return if (unit == null || stripUnit ) formatValue() else " ${formatValue()} $unit "
196200 }
197201
198202 fun formatValue (): String = getActualValue().toString()
0 commit comments