【问题标题】:How to convert a string to Double with all the numbers after the floating poing? [duplicate]如何将字符串转换为浮点数后所有数字的双精度数? [复制]
【发布时间】:2021-06-25 23:24:14
【问题描述】:

假设我有一个字符串“53.430996”,我想将其转换为字符串并在我尝试过的浮点数之后存储所有值:

#include <iostream>
#include <bits/stdc++.h>
#include <string>
#include <stdlib.h>
using namespace std;
int main(){
   char* convertme="53.430996";
   double converted=atof(convertme);
   cout << converted;
   return 0;
}

但是输出是 53.431 但我需要 53.430996 所以我可以稍后将其转换为 long long

static cast < long long > (coord ∗ 100000)

所以,我可以得到没有浮点 5343099 的值

【问题讨论】:

标签: c++ double long-long


【解决方案1】:

只需要确保为定点指定所需的精度。

#include <iomanip>
#include <iostream>
#include <string>

using std::cout;
using std::stod;
using std::fixed;
using std::setprecision;

int main(){
   auto convertme="53.430996";
   auto converted = stod(convertme);
   cout << fixed << setprecision(6) << converted << "\n";
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多