Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/io/manip/get money"

From cppreference.com
< cpp‎ | io‎ | manip
m (Use since= and until= params of {{ddcl}} template.)
m (headers sorted)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{{cpp/title|get_money}}
 
{{cpp/title|get_money}}
 
{{cpp/io/manip/navbar}}
 
{{cpp/io/manip/navbar}}
{{ddcl | header=iomanip | since=c++11 |1=
+
{{ddcl|header=iomanip|since=c++11|1=
 
template< class MoneyT >
 
template< class MoneyT >
 
/*unspecified*/ get_money( MoneyT& mon, bool intl = false );
 
/*unspecified*/ get_money( MoneyT& mon, bool intl = false );
 
}}
 
}}
  
When used in an expression {{c|in >> get_money(mon, intl)}}, parses the character input as a monetary value, as specified by the {{lc|std::money_get}} facet of the locale currently imbued in {{tt|in}}, and stores the value in {{tt|mon}}.
+
When used in an expression {{c|in >> get_money(mon, intl)}}, parses the character input as a monetary value, as specified by the {{lc|std::money_get}} facet of the locale currently imbued in {{c|in}}, and stores the value in {{c|mon}}.
  
This function behaves as a {{concept|FormattedInputFunction}}.
+
The extraction operation in {{c|in >> get_money(mon, intl)}} behaves as a {{named req|FormattedInputFunction}}.
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | mon | variable where monetary value will be written. Can be either {{c|long double}} or {{c|basic_string}}}}
+
{{par|mon|variable where monetary value will be written. Can be either {{c|long double}} or {{lc|std::basic_string}}}}
{{par | intl | expects to find required international currency strings if {{c|true}}, expects optional currency symbols otherwise}}  
+
{{par|intl|expects to find required international currency strings if {{c|true}}, expects optional currency symbols otherwise}}  
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
 
+
{{cpp/io/manip/iomanip return value|get_money}}
Returns an object of unspecified type such that if {{tt|in}} is the name of an output stream of type {{c|std::basic_istream<CharT, Traits>}}, then the expression {{c|in >> get_money(mon, intl)}} behaves as if the following code was executed:
+
 
+
{{c|1=
+
typedef std::istreambuf_iterator<CharT, Traits> Iter;
+
typedef std::money_get<CharT, Iter> MoneyGet;
+
std::ios_base::iostate err = std::ios_base::goodbit;
+
const MoneyGet &mg = std::use_facet<MoneyGet>(in.getloc());
+
mg.get(Iter(in.rdbuf()), Iter(), intl, in, err, mon);
+
if (std::ios_base::goodbit != err)
+
    out.setstate(err);
+
}}
+
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iomanip>
 
#include <iostream>
 
#include <iostream>
 +
#include <locale>
 
#include <sstream>
 
#include <sstream>
#include <locale>
+
 
#include <iomanip>
+
 
int main()
 
int main()
 
{
 
{
Line 43: Line 32:
 
     long double v1, v2;
 
     long double v1, v2;
 
     std::string v3;
 
     std::string v3;
 +
   
 
     in.imbue(std::locale("en_US.UTF-8"));
 
     in.imbue(std::locale("en_US.UTF-8"));
 
     in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
 
     in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
 
+
   
     std::cout << '"' << in.str() << "\" parsed as: "
+
     if (in)
              << v1 << ", " << v2 << ", " << v3 << '\n';
+
        std::cout << std::quoted(in.str()) << " parsed as: "
 +
                  << v1 << ", " << v2 << ", " << v3 << '\n';
 +
    else
 +
        std::cout << "Parse failed";
 
}
 
}
| output=
+
|output=
 
"$1,234.56 2.22 USD  3.33" parsed as: 123456, 222, 333
 
"$1,234.56 2.22 USD  3.33" parsed as: 123456, 222, 333
 
}}
 
}}
Line 55: Line 48:
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/locale/dsc money_get}}
+
{{dsc inc|cpp/locale/dsc money_get}}
{{dsc inc | cpp/io/manip/dsc put_money}}
+
{{dsc inc|cpp/io/manip/dsc put_money}}
 
{{dsc end}}
 
{{dsc end}}
  
[[de:cpp/io/manip/get money]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/io/manip/get money]]
+
[[fr:cpp/io/manip/get money]]
+
[[it:cpp/io/manip/get money]]
+
[[ja:cpp/io/manip/get money]]
+
[[pt:cpp/io/manip/get money]]
+
[[ru:cpp/io/manip/get money]]
+
[[zh:cpp/io/manip/get money]]
+

Latest revision as of 22:29, 15 September 2023

 
 
 
Input/output manipulators
Floating-point formatting
Integer formatting
Boolean formatting
Field width and fill control
Other formatting
Whitespace processing
Output flushing
(C++20)  

Status flags manipulation
Time and money I/O
get_money
(C++11)
(C++11)
(C++11)
(C++11)
Quoted manipulator
(C++14)
 
Defined in header <iomanip>
template< class MoneyT >
/*unspecified*/ get_money( MoneyT& mon, bool intl = false );
(since C++11)

When used in an expression in >> get_money(mon, intl), parses the character input as a monetary value, as specified by the std::money_get facet of the locale currently imbued in in, and stores the value in mon.

The extraction operation in in >> get_money(mon, intl) behaves as a FormattedInputFunction.

Contents

[edit] Parameters

mon - variable where monetary value will be written. Can be either long double or std::basic_string
intl - expects to find required international currency strings if true, expects optional currency symbols otherwise

[edit] Return value

An object of unspecified type such that

  • if in is an object of type std::basic_istream<CharT, Traits>, the expression in >> get_money(mon, intl)
    • has type std::basic_istream<CharT, Traits>&
    • has value in
    • behaves as if it called f(in, mon, intl)

where the function f is defined as:

template<class CharT, class Traits, class MoneyT>
void f(std::basic_ios<CharT, Traits>& str, MoneyT& mon, bool intl)
{
    using Iter = std::istreambuf_iterator<CharT, Traits>;
    using MoneyGet = std::money_get<CharT, Iter>;
 
    std::ios_base::iostate err = std::ios_base::goodbit;
    const MoneyGet& mg = std::use_facet<MoneyGet>(str.getloc());
 
    mg.get(Iter(str.rdbuf()), Iter(), intl, str, err, mon);
 
    if (err != std::ios_base::goodbit)
        str.setstate(err);
}

[edit] Example

#include <iomanip>
#include <iostream>
#include <locale>
#include <sstream>
 
int main()
{
    std::istringstream in("$1,234.56 2.22 USD  3.33");
    long double v1, v2;
    std::string v3;
 
    in.imbue(std::locale("en_US.UTF-8"));
    in >> std::get_money(v1) >> std::get_money(v2) >> std::get_money(v3, true);
 
    if (in)
        std::cout << std::quoted(in.str()) << " parsed as: "
                  << v1 << ", " << v2 << ", " << v3 << '\n';
    else
        std::cout << "Parse failed";
}

Output:

"$1,234.56 2.22 USD  3.33" parsed as: 123456, 222, 333

[edit] See also

parses and constructs a monetary value from an input character sequence
(class template) [edit]
(C++11)
formats and outputs a monetary value
(function template) [edit]