def _get_formatted_total(groups): calculated_total = 0 answer_format = {'type': None} for group in groups: for block in group['blocks']: for question in block['questions']: for answer in question['answers']: if not answer_format['type']: answer_format = { 'type': answer['type'], 'unit': answer.get('unit'), 'unit_length': answer.get('unit_length'), 'currency': answer.get('currency'), } answer_value = answer.get('value') or 0 calculated_total += answer_value if answer_format['type'] == 'currency': return get_formatted_currency(calculated_total, answer_format['currency']) if answer_format['type'] == 'unit': return format_unit(answer_format['unit'], calculated_total, answer_format['unit_length']) if answer_format['type'] == 'percentage': return format_percentage(calculated_total) return format_number(calculated_total)
def format_playback_value(value: Union[float, Decimal], currency: Optional[str] = None) -> str: if currency: return get_formatted_currency(value, currency) formatted_number: str = format_number(value) return formatted_number
def _get_formatted_total(self, groups, current_location): calculated_total = 0 answer_format = {"type": None} for group in groups: for block in group["blocks"]: question = choose_question_to_display( block, self._schema, self._metadata, self._response_metadata, self._answer_store, self._list_store, current_location=current_location, ) for answer in question["answers"]: if not answer_format["type"]: answer_format = { "type": answer["type"], "unit": answer.get("unit"), "unit_length": answer.get("unit_length"), "currency": answer.get("currency"), } answer_value = answer.get("value") or 0 calculated_total += answer_value if answer_format["type"] == "currency": return get_formatted_currency(calculated_total, answer_format["currency"]) if answer_format["type"] == "unit": return format_unit(answer_format["unit"], calculated_total, answer_format["unit_length"]) if answer_format["type"] == "percentage": return format_percentage(calculated_total) return format_number(calculated_total)
def format_playback_value(value, currency=None): if currency: return get_formatted_currency(value, currency) return format_number(value)
def test_get_formatted_currency_with_no_value(self): self.assertEqual(get_formatted_currency(""), "")