lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] valyuta/007 7b3debe3 07/10: Add a 'divisor' argument


From: Greg Chicares
Subject: [lmi-commits] [lmi] valyuta/007 7b3debe3 07/10: Add a 'divisor' argument to LedgerBase::value_str()
Date: Tue, 19 Apr 2022 19:00:41 -0400 (EDT)

branch: valyuta/007
commit 7b3debe3c778db1ba08f84c236c7909408b9e0be
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>

    Add a 'divisor' argument to LedgerBase::value_str()
    
    The motivation is to allow formatting integral cents as decimal dollars.
    
    It's not worth making this any prettier, because it's used only in one
    function; ideally only one routine (not this one) would be used for
    formatting all ledger data.
---
 ledger_base.cpp | 10 ++++++++--
 ledger_base.hpp |  2 ++
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/ledger_base.cpp b/ledger_base.cpp
index fb4c74f4..9f9d0b15 100644
--- a/ledger_base.cpp
+++ b/ledger_base.cpp
@@ -143,12 +143,15 @@ void LedgerBase::Copy(LedgerBase const& obj)
 std::string LedgerBase::value_str
     (std::string const& map_key
     ,int                index
+    ,double             divisor
     ) const
 {
+    LMI_ASSERT(0.0 != divisor);
+
     double_vector_map::const_iterator found = AllVectors.find(map_key);
     if(AllVectors.end() != found)
         {
-        return value_cast<std::string>((*(*found).second)[index]);
+        return value_cast<std::string>((*(*found).second)[index] / divisor);
         }
 
     alarum() << "Map key '" << map_key << "' not found." << LMI_FLUSH;
@@ -158,8 +161,11 @@ std::string LedgerBase::value_str
 //============================================================================
 std::string LedgerBase::value_str
     (std::string const& map_key
+    ,double             divisor
     ) const
 {
+    LMI_ASSERT(0.0 != divisor);
+
     string_map::const_iterator found_string = Strings.find(map_key);
     if(Strings.end() != found_string)
         {
@@ -169,7 +175,7 @@ std::string LedgerBase::value_str
     scalar_map::const_iterator found_scalar = AllScalars.find(map_key);
     if(AllScalars.end() != found_scalar)
         {
-        return value_cast<std::string>(*(*found_scalar).second);
+        return value_cast<std::string>(*(*found_scalar).second / divisor);
         }
 
     alarum() << "Map key '" << map_key << "' not found." << LMI_FLUSH;
diff --git a/ledger_base.hpp b/ledger_base.hpp
index e930ff2a..0cbbef0a 100644
--- a/ledger_base.hpp
+++ b/ledger_base.hpp
@@ -182,9 +182,11 @@ class LMI_SO LedgerBase
     std::string value_str
         (std::string const& map_key
         ,int                index
+        ,double             divisor = 1.0
         ) const;
     std::string value_str
         (std::string const& map_key
+        ,double             divisor = 1.0
         ) const;
 
     double_vector_map const& all_vectors() const;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]