Skip to content

Discuss more elegant way for formatting text as currency #1469

Open
@ayrtondenner

Description

This is the current code to format text as currency:

const auto textLenght = Text.length();
if (!Text.empty() && textLenght <= 3)
return Text;
auto isNumber = true;
for (const auto &ch : Text)
{
// Allowed chars in strings containing monetary values
if (!std::isdigit(ch) && ch != '.' && ch != ',' && ch != '-')
{
isNumber = false;
break;
}
}
if (!isNumber)
return Text;
auto formattedText = Text; // copy text for formatting
for (auto i = textLenght - 1; i > 0; i--)
{
if ((textLenght - i) % 3 == 0 && formattedText[i - 1] != '-')
formattedText.insert(i, ",");
}
return formattedText;

It's a very manual process of

  • checking characters that can appear in currency strings but shouldn't be considered for the digits count (minus, comma, etc)
  • checking string length
  • adding a comma every three digits, from right to left

I'm opening this request to discuss if there's a more elegant and appropriate way of doing this instead of reinventing the wheel like we have right now.

Metadata

Assignees

No one assigned

    Labels

    Better Design RequiredThe current implementation is not ideal. We'd be better off with a new design for a solution.Code QuerySomething doesn't seem right in the code. It may not be a bug, but it probably needs fixing!EnhancementThis is a request for something that makes OpenApoc work better or more intuitivelyFeature RequestThis is a request for a new thing to be added to OpenApocFurther Information RequiredThis cannot currently be replicated - please provide more information and savegame examplesGraphical User Interface (UI/GUI)A ticket that relates to how the user interacts with the game through the UIHelp WantedHelp is required in making this fix/feature complete. If you can code or test, please take a look!Quality of Life (QoL)A feature or change that makes playing the game more streamlinedRoadmapA an issue containing features that need to be completed for a "full" gaming experience.Translations / Language / TextSomething that relates to the way languages, numbers or text are handled in the game

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions