From ec88d6193483984a3a2f8351e40ff0994e7c0946 Mon Sep 17 00:00:00 2001 From: login Date: Sun, 19 Nov 2023 07:34:42 +0000 Subject: Added thousands separator to tcoin balance display Added thousands separators to tcoin balance display to make large balances easier to read. --- tcoin.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 70 insertions(+), 7 deletions(-) diff --git a/tcoin.cpp b/tcoin.cpp index a015f7f..2c728a2 100644 --- a/tcoin.cpp +++ b/tcoin.cpp @@ -377,6 +377,59 @@ std::string get_username() return username; } +void num_stream_thousands_sep(std::ostringstream& ss, long long int const& amount, char sep=',') +{ + std::ostringstream rev; + long long int reduced_amount = amount/1000; + int residue = amount % 1000; + bool residue_gte_100 = residue >= 100; + bool residue_gte_10 = residue >= 10; + bool residue_gte_1 = residue >= 1;; + + int num_residue_digits = residue_gte_100 + residue_gte_10 + residue_gte_1; + + int mini_reduced_amount = amount/10; + int mini_residue = amount % 10; + + do + { + for(int i=0; i 0) + rev << sep; + + residue = reduced_amount % 1000; + residue_gte_100 = residue >= 100; + residue_gte_10 = residue >= 10; + residue_gte_1 = residue >= 1; + + num_residue_digits = residue_gte_100 + residue_gte_10 + residue_gte_1; + mini_reduced_amount = reduced_amount/10; + mini_residue = reduced_amount % 10; + reduced_amount = reduced_amount/1000; + } while(reduced_amount > 0); + + for(int i=0; i= 0 ? true : false; if(!is_non_negative) //i.e., is negative ss << "-"; - if(((is_non_negative*2-1)*amount) % 100 == 0) - ss << (is_non_negative*2-1)*amount/100; - else if((is_non_negative*2-1)*amount % 100 < 10) - ss << (is_non_negative*2-1)*amount/100 << ".0" << (is_non_negative*2-1)*amount % 100; - else - ss << (is_non_negative*2-1)*amount/100 << "." << (is_non_negative*2-1)*amount % 100; - if(((is_non_negative*2-1)*amount == 100) && strcmp(appended_chars_singular, "")) + int amount_sign = is_non_negative*2-1; + long long int abs_amount = amount_sign * amount; + + bool amount_is_integer = abs_amount % 100 == 0; + bool amount_has_single_digit_cents = !amount_is_integer && (abs_amount % 100 < 10); + bool amount_has_double_digit_cents = !(amount_is_integer || amount_has_single_digit_cents); + + num_stream_thousands_sep(ss, abs_amount/100); + + if(amount_has_single_digit_cents) + ss << ".0"; + if(amount_has_double_digit_cents) + ss << "."; + if(!amount_is_integer) + ss << abs_amount % 100; + + if((abs_amount == 100) && strcmp(appended_chars_singular, "")) ss << appended_chars_singular; else ss << appended_chars_default; -- cgit 1.4.1-2-gfad0