• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Locale Support</title>
5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Chapter 1. Boost.Convert 2.0">
8<link rel="up" href="../stream_converter.html" title="boost::cnv::stream Converter">
9<link rel="prev" href="../stream_converter.html" title="boost::cnv::stream Converter">
10<link rel="next" href="supported_string_types.html" title="Supported String Types">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr>
14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
15<td align="center"><a href="../../../../../../../index.html">Home</a></td>
16<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
19<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
20</tr></table>
21<hr>
22<div class="spirit-nav">
23<a accesskey="p" href="../stream_converter.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stream_converter.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="supported_string_types.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="boost_convert.converters_detail.stream_converter.locale_support"></a><a class="link" href="locale_support.html" title="Locale Support">Locale
28        Support</a>
29</h4></div></div></div>
30<p>
31          Locale support is similar to the formatting support as demonstrated by
32          the following example:
33        </p>
34<p>
35</p>
36<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">cnv</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">;</span>
37<span class="keyword">namespace</span> <span class="identifier">arg</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">parameter</span><span class="special">;</span>
38</pre>
39<p>
40        </p>
41<p>
42</p>
43<pre class="programlisting">    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">cstream</span> <span class="identifier">cnv</span><span class="special">;</span>
44    <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span>  <span class="identifier">rus_locale</span><span class="special">;</span>
45    <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span>  <span class="identifier">eng_locale</span><span class="special">;</span>
46
47    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">eng_locale_name</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"English_United States.1251"</span> <span class="special">:</span> <span class="string">"en_US.UTF-8"</span><span class="special">;</span>
48    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span> <span class="identifier">rus_locale_name</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"Russian_Russia.1251"</span> <span class="special">:</span> <span class="string">"ru_RU.UTF-8"</span><span class="special">;</span>
49    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">rus_expected</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"1,235e-002"</span>  <span class="special">:</span> <span class="string">"1,235e-02"</span><span class="special">;</span>
50    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">eng_expected</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"1.235e-002"</span>  <span class="special">:</span> <span class="string">"1.235e-02"</span><span class="special">;</span>
51    <span class="keyword">char</span> <span class="keyword">const</span><span class="special">*</span>    <span class="identifier">dbl_expected</span> <span class="special">=</span> <span class="identifier">test</span><span class="special">::</span><span class="identifier">cnv</span><span class="special">::</span><span class="identifier">is_msc</span> <span class="special">?</span> <span class="string">"1.2345E-002"</span> <span class="special">:</span> <span class="string">"1.2345E-02"</span><span class="special">;</span>
52
53<span class="comment">//  cnv(std::setprecision(4))(std::uppercase)(std::scientific);</span>
54    <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">precision</span> <span class="special">=</span> <span class="number">4</span><span class="special">)</span>
55       <span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">uppercase</span> <span class="special">=</span> <span class="keyword">true</span><span class="special">)</span>
56       <span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">notation</span> <span class="special">=</span> <span class="identifier">cnv</span><span class="special">::</span><span class="identifier">notation</span><span class="special">::</span><span class="identifier">scientific</span><span class="special">);</span>
57
58    <span class="keyword">double</span> <span class="identifier">double_v01</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;(</span><span class="identifier">dbl_expected</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>
59    <span class="identifier">string</span> <span class="identifier">double_s02</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">double_v01</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad"</span><span class="special">);</span>
60
61    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">dbl_expected</span> <span class="special">==</span> <span class="identifier">double_s02</span><span class="special">);</span>
62
63    <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">rus_locale</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span><span class="special">(</span><span class="identifier">rus_locale_name</span><span class="special">);</span> <span class="special">}</span>
64    <span class="keyword">catch</span> <span class="special">(...)</span> <span class="special">{</span> <span class="identifier">printf</span><span class="special">(</span><span class="string">"Bad locale %s.\n"</span><span class="special">,</span> <span class="identifier">rus_locale_name</span><span class="special">);</span> <span class="identifier">exit</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="special">}</span>
65
66    <span class="keyword">try</span> <span class="special">{</span> <span class="identifier">eng_locale</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">locale</span><span class="special">(</span><span class="identifier">eng_locale_name</span><span class="special">);</span> <span class="special">}</span>
67    <span class="keyword">catch</span> <span class="special">(...)</span> <span class="special">{</span> <span class="identifier">printf</span><span class="special">(</span><span class="string">"Bad locale %s.\n"</span><span class="special">,</span> <span class="identifier">eng_locale_name</span><span class="special">);</span> <span class="identifier">exit</span><span class="special">(</span><span class="number">1</span><span class="special">);</span> <span class="special">}</span>
68
69<span class="comment">//  cnv(std::setprecision(3))(std::nouppercase);</span>
70    <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">precision</span> <span class="special">=</span> <span class="number">3</span><span class="special">)(</span><span class="identifier">arg</span><span class="special">::</span><span class="identifier">uppercase</span> <span class="special">=</span> <span class="keyword">false</span><span class="special">);</span>
71
72    <span class="identifier">string</span> <span class="identifier">double_rus</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">double_v01</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">rus_locale</span><span class="special">)).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad double_rus"</span><span class="special">);</span>
73    <span class="identifier">string</span> <span class="identifier">double_eng</span> <span class="special">=</span> <span class="identifier">convert</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">double_v01</span><span class="special">,</span> <span class="identifier">cnv</span><span class="special">(</span><span class="identifier">eng_locale</span><span class="special">)).</span><span class="identifier">value_or</span><span class="special">(</span><span class="string">"bad double_eng"</span><span class="special">);</span>
74
75    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">double_rus</span> <span class="special">==</span> <span class="identifier">rus_expected</span><span class="special">);</span>
76    <span class="identifier">BOOST_TEST</span><span class="special">(</span><span class="identifier">double_eng</span> <span class="special">==</span> <span class="identifier">eng_expected</span><span class="special">);</span>
77</pre>
78<p>
79        </p>
80</div>
81<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
82<td align="left"></td>
83<td align="right"><div class="copyright-footer">Copyright © 2009-2016 Vladimir Batov<p>
84        Distributed under the Boost Software License, Version 1.0. See copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>.
85      </p>
86</div></td>
87</tr></table>
88<hr>
89<div class="spirit-nav">
90<a accesskey="p" href="../stream_converter.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../stream_converter.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="supported_string_types.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
91</div>
92</body>
93</html>
94