xtd 0.2.0
parse_numeric.cpp

Shows how to use format xtd::parse method with numeric values (int8, int16, int32, int64, uint8, uint16, uint32 and uint64).

#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 i1 = xtd::parse<int>("42");
try {
auto i9 = xtd::parse<int>("Not number", number_styles::any);
} catch (const system_exception& e) {
console::write_line("Exception : {}", e.message());
}
}
// This code produces the following output :
//
// 42
// 42
// 42
// 42
// 42
// 42
// 42
// -42
// 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
int parse< int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.hpp:136
@ 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_parentheses
Indicates that the numeric string can have one pair of parentheses enclosing the number....
Definition number_styles.hpp:28
@ binary_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_binary_specifier styles are u...
Definition number_styles.hpp:56
@ octal_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_octal_specifier styles are us...
Definition number_styles.hpp:58
@ number
Indicates that the allow_leading_white, allow_trailing_white, allow_leading_sign, allow_trailing_sign...
Definition number_styles.hpp:46
@ hex_number
Indicates that the allow_leading_white, allow_trailing_white, and allow_hex_specifier styles are used...
Definition number_styles.hpp:54
@ e
The E key.
Definition console_key.hpp:96
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8