xtd 0.2.0
Loading...
Searching...
No Matches
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");
console::write_line(i1);
auto i2 = xtd::parse<int>("+42.8", number_styles::number);
console::write_line(i2);
auto i3 = xtd::parse<int>("0x2A", number_styles::hex_number);
console::write_line(i3);
auto i4 = xtd::parse<int>("052", number_styles::octal_number);
console::write_line(i4);
auto i5 = xtd::parse<int>("0b00101010", number_styles::binary_number);
console::write_line(i5);
auto i6 = xtd::parse<int>("$42", number_styles::currency);
console::write_line(i6);
auto i7 = xtd::parse<int>("$42", number_styles::currency);
console::write_line(i7);
auto i8 = xtd::parse<int>("(42)", number_styles::number | number_styles::allow_parentheses);
console::write_line(i8);
try {
auto i9 = xtd::parse<int>("Not number", number_styles::any);
console::write_line(i9);
} 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
The exception that is thrown when a method call is invalid for the object's current state.
Definition system_exception.h:18
int parse< int >(const std::string &str, number_styles styles)
Convert a string into a type.
Definition parse.h:137
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10