c++ - 浮点数 C++,如何在数组的每个元素末尾添加“f”
问题描述
我刚刚打开 filename.file ,将每个浮点数保存在向量“顶点”中,但它们保存为整数。需要将所有数字正确转换为浮点数。例如:从“1”到“1.00000f”或至少从“1”到“1.0f”。
/////filename.file
...
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
...
//////
std::vector<float> vertices;
std::ifstream file(filename);
std::string line;
while (std::getline(file, line))
{
std::istringstream iss(line);
std::string result;
if (std::getline(iss, result, ' '))
{
if (result == "v")
{
while (std::getline(iss, token, ' '))
{
std::istringstream iss1(token);
if (std::getline(iss1, word))
{
float word_float = std::stof(word);
//std::cout << word_float << std::endl;
vertices.push_back(word_float);
}
}
}
}
}
//Look what I got
for(std::size_t i = 0 ; i < vertices.size(); i++) {
std::cout << vertices[i] << " ";
}
/* 每个元素 1 -1 1 ...
// 但是这些数字应该完全保存
1.000000f -1.000000f 1.000000f ... */
解决方案
听起来您想要以下顺序的东西:
std::cout << std::fixed << std::setprecision(5) << value << "f";
例如:
float values[] = { 1.0f, -1.0f, 1.0f };
for (auto const &value : values)
std::cout << std::fixed << std::setprecision(5) << value << "f\t";
结果:
1.00000f -1.00000f 1.00000f
推荐阅读
- vue.js - AG-grid Vue 不适用于 vuejs 版本 3
- ios - react-native 不在 ios 14 上运行应用程序
- spring - 如何使用 Spring-Boot、MySQL 和 Thymeleaf 更新登录用户的用户详细信息?
- python - re.search if else 语句和replace()
- python - requests GET 请求需要很长时间才能得到响应
- python - 如何在文件夹结构中读取多个 json 文件?
- mysql - 通过 PHPMyAdmin 导入数据库表和数据需要很长时间
- flutter - 孩子无法对手势做出反应
- reactjs - 连接到 wifi 网络时 React Native 扫描打印机并获取打印机的 IP 地址
- python - 如何使用 python 抓取亚马逊的多个搜索结果页面?