【发布时间】:2023-03-15 00:50:01
【问题描述】:
我正在尝试从小数中删除尾随零,如果没有更多尾随零,则删除小数。
这个字符串是从 boost 的 gmp_float 字符串输出中产生的。
这是我的尝试,但我收到了std::out_of_range:
string trim_decimal( string toFormat ){
while( toFormat.find(".") && toFormat.substr( toFormat.length() - 1, 1) == "0" || toFormat.substr( toFormat.length() - 1, 1) == "." ){
toFormat.pop_back();
}
return toFormat;
}
如果存在小数点,如何删除尾随 0s,如果小数点后没有更多 0s,如何删除小数点?
【问题讨论】:
标签: c++ string zero decimal-point trailing