GrpcPrint/PrintS/utils/ConverType.hpp

84 lines
2.3 KiB
C++
Raw Normal View History

#pragma once
#include <string>
#include <stdexcept>
class ConverType
{
public:
static int TryToI(const std::string& input) {
int ret = INT_MIN;
try {
ret = stoi(input);
return ret;
}
catch (const std::invalid_argument& e) {
printf("input is not number...error:%s\n", e.what());
}
catch (const std::out_of_range& e) {
printf("input number is out of int range...,error:%s\n",e.what());
}
return ret;
}
static bool IsToI(const std::string& input) {
try {
int ret = stoi(input);
return true;
}
catch (const std::invalid_argument& e) {
printf("input is not number...error:%s\n", e.what());
}
catch (const std::out_of_range& e) {
printf("input number is out of int range...error:%s\n", e.what());
}
return false;
}
static float TryToF(const std::string& input) {
float ret = -1.0;
try {
ret = stof(input);
return ret;
}
catch (const std::invalid_argument& e) {
printf("input is not float...error:%s\n", e.what());
}
catch (const std::out_of_range& e) {
printf("input number is out of float range...error:%s\n", e.what());
}
return ret;
}
2024-05-28 18:07:35 +08:00
static double TryToD(const std::string& input) {
double ret = -1.0;
try {
ret = stod(input);
return ret;
}
catch (const std::invalid_argument& e) {
printf("input is not double...error:%s\n", e.what());
}
catch (const std::out_of_range& e) {
printf("input number is out of double range...error:%s\n", e.what());
}
return ret;
}
2024-05-30 11:18:10 +08:00
static long long TryToLL(const std::string& input) {
long long ret = -1;
try {
ret = stoll(input);
return ret;
}
catch (const std::invalid_argument& e) {
printf("input is not long long...error:%s\n", e.what());
}
catch (const std::out_of_range& e) {
printf("input number is out of long long range...error:%s\n", e.what());
}
return ret;
}
};