xtd 0.2.0
parse_floating_point.cpp

Shows how to use format xtd::parse method with floating point values (float, double and decimal).

#include <xtd/console>
#include <xtd/parse>
using namespace xtd;
auto main() -> int {
std::locale::global(std::locale {"en_US.UTF-8"}); // change to us for currency
auto d1 = xtd::parse<double>("12.345");
auto d4 = xtd::parse<double>("inf");
auto d5 = xtd::parse<double>("-inf");
auto d6 = xtd::parse<double>("nan");
try {
auto d7 = xtd::parse<double>("Not floating point", number_styles::any);
} catch (const system_exception& e) {
console::write_line("Exception : {}", e.message());
}
}
// This code produces the following output :
//
// 2.345
// 12.345
// 12.345
// inf
// -inf
// nan
// Exception : invalid character found
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.hpp:18
double parse< double >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:208
@ any
Indicates that all styles except allow_binary_specifier, allow_octal_specifier and allow_hex_specifie...
Definition number_styles.hpp:52
@ currency
Indicates that all styles except allow_exponent, allow_hex_specifier, allow_binary_specifier and allo...
Definition number_styles.hpp:50
@ allow_exponent
Indicates that the numeric string can be in exponential notation. The allow_exponent flag allows the ...
Definition number_styles.hpp:34
@ number
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_trailing_sign...
Definition number_styles.hpp:46
@ d6
The 6 key.
Definition console_key.hpp:80
@ d1
The 1 key.
Definition console_key.hpp:70
@ d4
The 4 key.
Definition console_key.hpp:76
@ d2
The 2 key.
Definition console_key.hpp:72
@ d5
The 5 key.
Definition console_key.hpp:78
@ e
The E key.
Definition console_key.hpp:96
@ d3
The 3 key.
Definition console_key.hpp:74
@ d7
The 7 key.
Definition console_key.hpp:82
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8