我需要将一个数字的小数部分转换为整数而不用逗号,例如我有3.35我想得到35个没有零或逗号的部分,
因为我使用modf()函数来提取小数部分,但是如果有任何方法可以提供0.35或者过滤'0'.如果您告诉我如何使用更小的代码,我将非常感激,
Arm*_*yan -1
我就是这样做的。
#include <sstream>
#include <string>
int main()
{
double d = yourDesiredNumber; //this is your number
std::ostringstream out;
out << setprecision(yourDesiredPrecision) << std::fixed
<< std::showpoint << d;
std::istringstream in(out.str());
std::string wholePart; //you won't need this.
int fractionalPart;
std::getline(in, wholePart, '.');
in >> fractionalPart;
//now fractionalPart contains your desired value.
}
Run Code Online (Sandbox Code Playgroud)
我很确定,您可以只使用一个对象,而不是两个不同的istringstream和对象,但我不确定细节(从未使用过该类),所以我没有在示例中使用它。ostringstreamstringstream