GrpcPrint/PrintS/utils/ConverType.hpp

54 lines
1.4 KiB
C++

#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;
}
};